这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

如何优雅的基于 element-plus,封装一个梦中情 dialog

优点

摆脱繁琐的 visible 的命名,以及反复的重复 dom。

想法

将 dialog 封装成一个函数就能唤起的组件。如下:

addDialog({
title: "测试", //弹窗名
component: TestVue, //组件
width: "400px", //弹窗大小
props: {
//传给组件的参数
id: 0
},
callBack: (data: any) => {
//当弹窗任务结束后,调用父页面的回掉函数。(比如我新增完成了需要刷新列表页面)
console.log("回调函数", data)
}
})

效果图

基于 el-dialog 进行初步封装

// index.ts
import { reactive } from "vue"
type dialogOptions = {
title: string
component: any
props?: Object
width: string
visible?: any
callBack?: Function
}
export const dialogList: dialogOptions[] = reactive([]) export const addDialog = (options: dialogOptions) => {
dialogList.push(Object.assign(options, { visible: true }))
} export const closeDialog = (item: dialogOptions, i: number, args?: any, isNativeClose?: boolean) => {
dialogList.splice(i, 1)
if (!isNativeClose) item.callBack && item.callBack(...args)
}
<template>
<Teleport to="body">
<el-dialog
v-for="(item, index) in dialogList"
:key="index"
:title="item.title"
:width="item.width"
v-model="item.visible"
@close="() => closeDialog(item, index, '', true)"
>
<component :is="item.component" v-bind="item.props" @close="(...args:any) => closeDialog(item, index, args)" />
</el-dialog>
</Teleport>
</template> <script setup lang="ts">
import { dialogList, closeDialog } from "./index"
</script>
  • 首先定义了 dialogList,它包含了所有弹窗的信息。
  • component 使用 componet is 去动态加载子组件
  • addDialog 调用唤起弹窗的函数
  • closeDialog 关闭弹窗的函数

在app.vue中挂载

<script setup>
import Mydialog from "@/components/gDialog/index.vue"
</script> <template>
<router-view />
<Mydialog></Mydialog>
</template> <style scoped> </style>

使用

创建一个弹窗组件

<!-- test.vue -->
<template>
父弹窗
<el-button type="primary" @click="openChildDialog">打开子dialog</el-button>
<el-button type="primary" @click="closeDialog">关闭弹窗</el-button>
</template> <script setup lang="ts">
import { addDialog } from "@/components/gDialog/index"
import childVue from "./child.vue"
const props = defineProps(["id"])
console.log(props.id, "props")
const emit = defineEmits(["close"])
const closeDialog = () => {
emit("close", 1, 2, 34)
}
const openChildDialog = () => {
addDialog({
title: "我是子dialog",
width: "500px",
component: childVue
})
}
</script>

在列表页面唤醒弹窗

<!-- list.vue -->
<template>
列表页
<el-button type="primary" @click="openDialog">打开dialog</el-button>
</template>
<script setup lang="ts">
import { addDialog } from "@/components/gDialog/index"
import TestDialog from "./test.vue"
const openDialog = () => {
addDialog({
title: "我是dialog",
width: "500px",
props:{
id:0
}
component: TestDialog,
callBack: (data: any) => {
//当弹窗任务结束后,调用父页面的回掉函数。(比如我新增完成了需要刷新列表页面)
console.log("回调函数", data)
}
})
}

多层级弹窗嵌套

<!-- child.vue -->
<template>
子弹窗
<el-button type="primary" @click="closeDialog">关闭弹窗</el-button>
</template> <script setup lang="ts">
import { addDialog } from "@/components/gDialog/index"
const emit = defineEmits(["close"])
const closeDialog = () => {
emit("close", 1, 2, 34)
}
</script>

附上代码

代码

本文转载于:

https://juejin.cn/post/7224007334514638903

如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。

记录--vue3优雅的使用element-plus的dialog的更多相关文章

  1. 关于Element对话框组件Dialog在使用时的一些问题及解决办法

    Element对话框组件Dialog在我们的实际项目开发中可以说是一个使用频率较高的组件,它能为我们展示提示的功能,如:业务模块提交前展示我们曾经输入或选择过的业务信息,或者展示列表信息中某项业务的具 ...

  2. (错误记录)Vue: Unknown custom element

    错误: vue.js:634 [Vue warn]: Unknown custom element: <ve-pie> - did you register the component c ...

  3. element ui里dialog关闭后清除验证条件

    //vue <!--添加用户dialog begin--> <el-dialog title="编辑用户" :visible.sync="dialogF ...

  4. element ui中dialog相关问题

    一,今天需要在dialog里面引入另一个页面,就是打开dialog显示该页面(把页面放到dialog中),引入的语句如下: <iframe src="view?path=rkdj_b& ...

  5. element模态框dialog中的select组件中选中无反应无显示

    https://blog.csdn.net/PGguoqi/article/details/90240650 在vue里,当你对一个不存在的属性或对象直接“.”进行赋值,或者对数组不存在的索引项直接用 ...

  6. vue3项目,记录我是如何用1h实现产品预估1天工作量的界面需求

    最近在编写前端界面,硬是一人一周时间加班加点写完了一个项目的前端界面(一级菜单有12个页面+一个控制台大屏,二三级界面有N个),之前预估前端界面的编写需要一个月,我是自己把自己卷死了(没有办法,项目经 ...

  7. Vue3中无法为el-tree-select设置反选问题分析

    好久没有写博客了,刚好上周遇到一个难缠问题,这里记录一下. 环境:Vue3.2.Element Plus 问题:子组件 setting.vue => 弹窗组件 Dialog => 树选择组 ...

  8. 基于 vite 创建 vue3 全家桶项目(vite + vue3 + tsx + pinia)

    vite 最近非常火,它是 vue 作者尤大神发布前端构建工具,底层基于 Rollup,无论是启动速度还是热加载速度都非常快.vite 随 vue3 正式版一起发布,刚开始的时候与 vue 绑定在一起 ...

  9. 【vue】vue +element 搭建及开发中项目中,遇到的错误提示

    1. import Layout from '@/views/layout/Layout'; export default [ { // 配置路由,当路径为'/activePublic',使用组件ac ...

  10. 3java面试题 传智 发的 有用

    第一章内容介绍 20 第二章JavaSE基础 21 一.Java面向对象 21 1. 面向对象都有哪些特性以及你对这些特性的理解 21 2. 访问权限修饰符public.private.protect ...

随机推荐

  1. JOISC 2023 纪录

    记录一下 JOISC 2023 的做题记录 Day1 T1 Two Currencies 给定一棵树,在边上有总计 \(m\) 个检查站,经过一个检查站需要叫 \(1\) 枚金币或者若干枚银币.\(Q ...

  2. 使用winhex查看FAT16格式结构

    winhex介绍 winhex可以直接查看磁盘二进制信息, 可以比较直观地查看到各种文件系统格式的区别. winhex使用 查看硬盘要管理员权限, 即启动的时候要用邮件管理员权限启动 点击Tools- ...

  3. BUG管理系统(Mantis)迁移实战

    Mantis迁移实战 名词解释 Mantis:  开源的BUG管理平台Mantis,也做MantisBT.           同档次产品有EasyBUG,QC,BugFree,Bugzila. Xa ...

  4. 升级 vcpkg 遇到的一些坑

    项目上有个需求要用到 wil 库,于是打开 cmd 输入: vcpkg install wil:x86-windows-static 等了很久,一直卡在配置命令 连续试了好几遍,还是不行,安装其他的静 ...

  5. 第一百一十三篇: JS数组Array(二)数组方法 栈、队列、排序

    好家伙,    在上一篇中,我们知道了, JS的数组中每个槽位可以存储任意类型的数据 那么,我们能通过数组去模仿某些数据结构吗? 答案是肯定的 1.栈方法 ECMAScript 给数组提供几个方法,让 ...

  6. 【Azure App Service】如何来停止 App Service 的高级工具站点 Kudu ?

    问题描述 如何来停止 App Service 的高级工具站点 Kudu ? kudu 介绍 Kudu 提供了一组面向开发人员的工具和扩展点,用于您的应用服务应用程序. Kudu (Advanced T ...

  7. 【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM

    问题描述 在Azure的Spring Cloud服务 (官名为:Spring Apps)中,在Metrics 页面中查看 App Memory Usage 和 jvm.memory.use,发现两则在 ...

  8. 【Azure 应用服务】添加自定义域时,Domain ownership 验证无法通过 

    问题描述 在Azure App Service添加自定义域名时,遇见了Domain ownership 验证无法通过的问题? 问题解决 因为DNS中配置App Service默认域名和自定义域名的CN ...

  9. centos7挂载硬盘(大于2T)

    配置方法: 1.root账户下,执行 fdisk -l 命令查看挂载的硬盘设备,假设设备号为/dev/sdb,接下来我们使用parted命令来进行GPT分区 2.使用parted命令进行GPT分区 # ...

  10. putty配置kali linux 远程连接

    首先配置sshd_config文件 VI 编辑或者使用 gedit 文本编辑, 修改的内容包括下面几个 红色标出(为了以复原建议大家 拷贝一份或者修改的地方进行标注) 之后重启服务,但是有的还是存在报 ...