记录--vue3优雅的使用element-plus的dialog
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助
如何优雅的基于 element-plus,封装一个梦中情 dialog
优点
摆脱繁琐的 visible 的命名,以及反复的重复 dom。
想法
将 dialog 封装成一个函数就能唤起的组件。如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | addDialog({ title: "测试", //弹窗名 component: TestVue, //组件 width: "400px", //弹窗大小 props: { //传给组件的参数 id: 0 }, callBack: (data: any) => { //当弹窗任务结束后,调用父页面的回掉函数。(比如我新增完成了需要刷新列表页面) console.log("回调函数", data) } }) |
效果图
基于 el-dialog 进行初步封装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // 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) } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | < 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中挂载
1 2 3 4 5 6 7 8 9 10 11 12 | < script setup> import Mydialog from "@/components/gDialog/index.vue" </ script > < template > < router-view /> < Mydialog ></ Mydialog > </ template > < style scoped> </ style > |
使用
创建一个弹窗组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!-- 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 > |
在列表页面唤醒弹窗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!-- 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) } }) } |
多层级弹窗嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!-- 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 > |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-05-17 TP6框架--EasyAdmin学习笔记:定义路由