vue3怎么使用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) => { dialogList.splice(i, 1) 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" > <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>
以上是vue3怎么使用element-plus的dialog的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

如何通过vue和Element-plus实现表格的可编辑和行选择引言:在开发Web应用程序时,表格是经常使用的组件之一。而表格的可编辑和行选择功能是很常见和实用的需求。在Vue.js框架中,结合Element-plus组件库可以轻松实现这两个功能。本文将介绍如何通过Vue和Element-plus实现表格的可编辑和行选择功能,并提供相应的代码示例。一、项目准

vue3+vite:src使用require动态导入图片报错和解决方法vue3+vite动态的导入多张图片vue3如果使用的是typescript开发,就会出现require引入图片报错,requireisnotdefined不能像使用vue2这样imgUrl:require(’…/assets/test.png’)导入,是因为typescript不支持require所以用import导入,下面介绍如何解决:使用awaitimport

如何利用Vue和ElementPlus实现分步表单和表单校验在Web开发中,表单是非常常见的用户交互组件之一。而对于复杂的表单,我们常常需要进行分步填写以及表单校验的功能。本文将介绍如何利用Vue和ElementPlus框架来实现这两个功能。一、分步表单分步表单指的是将一个大表单分割为几个小步骤,用户需要按照步骤进行填写。我们可以利用Vue的组件化和路由

如何使用Vue和ElementPlus实现上传和下载文件功能引言:在Web应用程序中,文件的上传和下载功能非常常见。本文将介绍如何使用Vue和ElementPlus来实现文件的上传和下载功能。通过示例代码,可以简单直观地了解如何使用Vue和ElementPlus来实现这些功能。一、安装和导入ElementPlus安装ElementPlus在Vue项

想要实现页面的局部刷新,我们只需要实现局部组件(dom)的重新渲染。在Vue中,想要实现这一效果最简便的方式方法就是使用v-if指令。在Vue2中我们除了使用v-if指令让局部dom的重新渲染,也可以新建一个空白组件,需要刷新局部页面时跳转至这个空白组件页面,然后在空白组件内的beforeRouteEnter守卫中又跳转回原来的页面。如下图所示,如何在Vue3.X中实现点击刷新按钮实现红框范围内的dom重新加载,并展示对应的加载状态。由于Vue3.X中scriptsetup语法中组件内守卫只有o

如何利用Vue和ElementPlus实现消息通知和弹窗提示简介:在Web应用开发中,消息通知和弹窗提示是非常重要的功能之一。Vue作为一种流行的前端框架,结合ElementPlus这个优秀的UI库,能够轻松地实现各种弹窗提示和消息通知的功能。本文将介绍如何在Vue项目中使用ElementPlus组件库来实现消息通知和弹窗提示功能,并附上相关代码示例。

如何利用Vue和ElementPlus实现数据的导出和打印功能近年来,随着前端开发的迅速发展,越来越多的网页应用需要提供数据导出和打印功能,以满足用户对数据的多样化使用需求。Vue作为一种流行的JavaScript框架,配合ElementPlus组件库的使用,可以轻松实现数据的导出和打印功能。本文将介绍一种基于Vue和ElementPlus的数据导出和

最终效果安装VueCropper组件yarnaddvue-cropper@next上面的安装值针对Vue3的,如果时Vue2或者想使用其他的方式引用,请访问它的npm官方地址:官方教程。在组件中引用使用时也很简单,只需要引入对应的组件和它的样式文件,我这里没有在全局引用,只在我的组件文件中引入import{userInfoByRequest}from'../js/api'import{VueCropper}from'vue-cropper&
