1、首先创建conform.vue,其内容如下:

<template><div class="xtx-confirm" :class="{fade}"><div class="wrapper" :class="{fade}"><div class="header"><h3>{{title}}</h3>
<!--        <a @click="cancel" href="JavaScript:;" class="iconfont icon-close-new">x</a>--><a @click="cancel" href="JavaScript:;" >x</a></div><div class="body"><i class="iconfont icon-warning"></i><span>{{text}}</span></div><div class="footer"><span @click="cancel" class="cancel">取消</span><span @click="submit" class="submit">确认</span></div></div></div>
</template>
<script setup>
import { onMounted, ref } from 'vue'const props = defineProps({title: {type: String,default: '温馨提示'},text: {type: String,default: ''},// 传递函数进来cancelCallback: {type: Function},submitCallback: {type: Function}
});// 对话框默认隐藏
const fade = ref(false)
// 组件渲染完毕后
onMounted(() => {// 过渡效果需要在元素创建完毕后延时一会加上才会触发setTimeout(() => {fade.value = true}, 0)
})
// 取消
const cancel = () => {// 其他事情props.cancelCallback()
}
// 确认
const submit = () => {// 其他事情props.submitCallback()
}// 下边这种不是setup的写法也可
// export default {//   name: 'conform',
//   props: {//     title: {//       type: String,
//       default: '温馨提示'
//     },
//     text: {//       type: String,
//       default: ''
//     },
//     cancelCallback: {//       type: Function
//     },
//     submitCallback: {//       type: Function
//     }
//   },
//   setup (props) {//     // 对话框默认隐藏
//     const fade = ref(false)
//     // 组件渲染完毕后
//     onMounted(() => {//       // 过渡效果需要在元素创建完毕后延时一会加上才会触发
//       setTimeout(() => {//         fade.value = true
//       }, 0)
//     })
//     // 取消
//     const cancel = () => {//       // 其他事情
//       props.cancelCallback()
//     }
//     // 确认
//     const submit = () => {//       // 其他事情
//       props.submitCallback()
//     }
//     return { cancel, submit, fade }
//   }
// }
</script>
<style scoped lang="less">
.xtx-confirm {position: fixed;left: 0;top: 0;width: 100%;height: 100%;z-index: 8888;background: rgba(0,0,0,0);//background: rgba(0,0,0,.5);&.fade {transition: all 0.4s;background: rgba(0,0,0,.5);}.wrapper {width: 400px;background: #fff;border-radius: 4px;position: absolute;top: 50%;left: 50%;transform: translate(-50%,-60%);opacity: 0;//opacity: 1;&.fade {transition: all 0.4s;transform: translate(-50%,-50%);opacity: 1;}.header,.footer {height: 50px;line-height: 50px;padding: 0 20px;}.body {padding: 20px 40px;font-size: 16px;.icon-warning {color: red;margin-right: 3px;font-size: 16px;}}.footer {text-align: right;cursor: pointer;.cancel{margin-right: 20px;cursor: pointer;}.submit{cursor: pointer;}//.xtx-button {//  margin-left: 20px;//}}.header {position: relative;h3 {font-weight: normal;font-size: 18px;}a {position: absolute;right: 15px;top: 15px;font-size: 20px;width: 20px;height: 20px;line-height: 20px;text-align: center;color: #999;&:hover {color: #666;}}}}
}
</style>

2、编写conform.js其内容如下:

import { createVNode, render } from 'vue'
import conform from './components/conform.vue'// 准备一个DOM
const div = document.createElement('div')
// div.setAttribute('class', 'xtx-confirm-container')
document.body.appendChild(div)// 返回的是promise对象,点取消销毁组件,点确认销毁组件
export default ({ title, text }) => {// 1. 导入被创建的组件// 2. 使用createVNode创建虚拟节点// 3. 准备一个dom容器装载组件// 4. 使用render函数渲染组件到容器return new Promise((resolve, reject) => {// 点击取消触发的函数const cancelCallback = () => {render(null, div)// reject(new Error('点击取消'))reject("取消")}// 点击确认触发的函数const submitCallback = () => {render(null, div)resolve('确认')}//  不光传递字段,还可以传递函数const vn = createVNode(conform, { title, text, cancelCallback, submitCallback })render(vn, div)})
}

3、在app.vue中使用,其内容如下:

<template><span @click="sure">点击弹出遮罩/确认按钮</span>
</template><script setup>
import Conform from "@/conform";
// import {ref} from "vue";
const sure = () => {Conform({text:"确认删除吗?"}).then((e)=>{console.log(e)}).catch((e)=>{console.log(e)})
}
</script><style scoped lang="less">
</style>

4、效果如下:

vue 弹出自定义确认取消对话框使用createVNode, render相关推荐

  1. QT TextEdit设置背景、明文加密、弹出网站、弹出自定义对话框、gif动态图片、程序启动动画、打包程序、关闭事件

    TextEdit设置背景 前提必须已添加了资源图片 铭文加密 ui->passLineEdit->setEchoMode(QLineEdit::Password); 弹出网站 #inclu ...

  2. js 弹出确认 取消对话框

    文章目录 js 弹出确认 取消对话框 一种: <a href="javascript:if(confirm('确实要删除该内容吗?'))location='http://www.goo ...

  3. wpf 点击按钮弹出新对话框_WPF弹出自定义窗口的方法

    本文实例讲述了WPF弹出自定义窗口的方法.分享给大家供大家参考,具体如下: 测试环境: [1]VS2010SP1 [2]WPF(.NET Framework 4)项目 内容简介 WPF工程如何弹出自定 ...

  4. ConfirmCancelUtilDialog【确认取消对话框封装类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 对确认取消对话框的封装. 前提:调用对话框的Activity必须继承FragmentActivity及其子类(比如AppCompat ...

  5. 【WPF】右下角弹出自定义通知样式(Notification)——简单教程

    [WPF]右下角弹出自定义通知样式(Notification)--简单教程 原文:[WPF]右下角弹出自定义通知样式(Notification)--简单教程 1.先看效果 2.实现 1.主界面是Mai ...

  6. Winform像菜单一样弹出自定义内容实现示例

    在实际工作中,如果能像菜单一样弹出自定义内容,会方便很多,比如查询时,比如下拉列表显示多列信息时,比如在填写某个信息需要查看一些信息树时.这个时候自定义弹出界面就显的非常重要了 我这里其实用到的是网上 ...

  7. 对div实现右键弹出自定义菜单

    实现只对蓝色的Div弹出自定义菜单,其他位置弹出默认的浏览器菜单 js代码如下: <script type="text/javascript">$(function() ...

  8. 微信小程序-弹出自定义对话框

    最近,需要在小程序里做一个弹出对话框的操作,微信提供的 showmodal 也达不到我想要的效果,于是自己尝试... 如图: 大神看了莫喷,希望能帮助到刚入门的小伙伴... 代码 : wxml: &l ...

  9. 弹出html对话框样式,对话框或弹出自定义样式的3种方法

    1.使用Activity实现 在AndroidManifest.xml中修改Actity的主题android:theme="@style/MyDialogStyleBottom" ...

最新文章

  1. php redis 安装和使用
  2. 在Windows 2003 64位下ASP无法连接Access数据库的解决办法
  3. MySql中添加用户,新建数据库,用户授权,删除用户,修改密码
  4. linux一切对象皆文件,为什么说Linux下“一切皆文件”?
  5. one thread one loop 思想
  6. CodeForces - 1339C Powered Addition(思维+贪心)
  7. Exceptions(小节)
  8. 系统测试集成测试单元测试_单元和集成测试的代码覆盖率
  9. Pytorch LSTM初识(详解LSTM+torch.nn.LSTM()实现)1
  10. python将一组数据转化为列表_Pandas将列表(List)转换为数据框(Dataframe)
  11. Linux添加/删除用户和用户组(linux中,添加cvs用户,实质就是添加linux用户。)
  12. ApacheCon Asia 2021 重磅开启!流处理专题 Call for Speaker
  13. Confluence 6 查看你的许可证细节
  14. camtasia studio2022新版本高清录屏编辑无水印
  15. 2021年苹果手机绕ID解锁教程-附使用工具
  16. mysql数据库地址 名称_数据库地址和名称是什么?怎么知道自己地址和名称?
  17. Pixelmator Pro 2.1.2 图像处理软件
  18. Java 线程池及参数动态调节详解
  19. 微信好友删除了怎么找回来
  20. SMS-EMOA:基于超体积支配的多目标选择

热门文章

  1. Android如何将软键盘回车换成搜索等按钮,EditText中imeOptions属性的使用
  2. 华为OD机试真题大全,用 Python 解华为机试题 | 机试宝典
  3. 1024,程序员福利是这些??
  4. 智能网联汽车ASIL安全等级如何划分
  5. 六年级4月27日——5月1日课程表
  6. 深入解析美团外卖Flutter-架构演进之路(上篇),2021Android研发必问高级面试题
  7. 英伟达显卡算力查询--A5000、RTX30系列以及GTX
  8. 小程序商城|微信小程序商城系统
  9. 程序设计第五单元知识点
  10. CDISC SDTM EG domain学习笔记