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脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

如何透過vue和Element-plus實作表格的可編輯和行選擇引言:在開發Web應用程式時,表格是經常使用的元件之一。而表格的可編輯和行選擇功能是很常見和實用的需求。在Vue.js框架中,結合Element-plus元件庫可以輕鬆實現這兩個功能。本文將介紹如何透過Vue和Element-plus實作表格的可編輯和行選擇功能,並提供對應的程式碼範例。一、專案準

如何利用Vue和ElementPlus實現逐步表單和表單校驗在Web開發中,表單是非常常見的使用者互動元件之一。而對於複雜的表單,我們常常需要進行逐步填寫以及表單校驗的功能。本文將介紹如何利用Vue和ElementPlus框架來實現這兩個功能。一、逐步表單逐步表單指的是將一個大表單分割為幾個小步驟,使用者需要依照步驟填寫。我們可以利用Vue的組件化和路由

如何使用Vue和ElementPlus實作上傳和下載檔案功能引言:在網路應用程式中,檔案的上傳和下載功能非常常見。本文將介紹如何使用Vue和ElementPlus來實現檔案的上傳和下載功能。透過範例程式碼,可以簡單直觀地了解如何使用Vue和ElementPlus來實現這些功能。一、安裝與導入ElementPlus安裝ElementPlus在Vue項

vue3+vite:src使用require動態導入圖片報錯和解決方法vue3+vite動態的導入多張圖片vue3如果使用的是typescript開發,就會出現require引入圖片報錯,requireisnotdefined不能像使用vue2這樣imgUrl:require(' …/assets/test.png')導入,是因為typescript不支援require所以用import導入,下面介紹如何解決:使用awaitimport

想要實現頁面的局部刷新,我們只需要實現局部元件(dom)的重新渲染。在Vue中,想要實現這效果最簡單的方式方法就是使用v-if指令。在Vue2中我們除了使用v-if指令讓局部dom的重新渲染,也可以新建一個空白元件,需要刷新局部頁面時跳轉至這個空白元件頁面,然後在空白元件內的beforeRouteEnter守衛中又跳轉回原來的頁面。如下圖所示,如何在Vue3.X中實現點擊刷新按鈕實現紅框範圍內的dom重新加載,並展示對應的加載狀態。由於Vue3.X中scriptsetup語法中組件內守衛只有o

如何利用Vue和ElementPlus實現訊息通知和彈跳窗提示簡介:在網路應用開發中,訊息通知和彈跳窗提示是非常重要的功能之一。 Vue作為一個受歡迎的前端框架,結合ElementPlus這個優秀的UI函式庫,能夠輕鬆地實現各種彈跳窗提示和訊息通知的功能。本文將介紹如何在Vue專案中使用ElementPlus元件庫來實作訊息通知和彈跳窗提示功能,並附上相關程式碼範例。

如何利用Vue和ElementPlus實現資料的匯出和列印功能近年來,隨著前端開發的快速發展,越來越多的網頁應用程式需要提供資料匯出和列印功能,以滿足使用者對資料的多樣化使用需求。 Vue作為一種流行的JavaScript框架,配合ElementPlus元件庫的使用,可以輕鬆實現資料的匯出和列印功能。本文將介紹一種基於Vue和ElementPlus的資料匯出和

vue3+ts+axios+pinia實作無感刷新1.先在專案中下載aiXos和pinianpmipinia--savenpminstallaxios--save2.封裝axios請求-----下載js-cookienpmiJS-cookie-s//引入aixosimporttype{AxiosRequestConfigig ,AxiosResponse}from"axios";importaxiosfrom'axios';import{ElMess
