Vue2+Elementui?Dialog實(shí)現(xiàn)封裝自定義彈窗組件
前言
在日常的管理系統(tǒng)界面中,我們寫的最多的除了列表表格之外,就是各種彈窗組件,如果將彈窗組件寫在上下文的話,每個(gè)頁面都需要編寫大量重復(fù)的dialog代碼已經(jīng)頻繁的創(chuàng)建控制顯示隱藏的變量信息,即使將彈窗內(nèi)展示信息抽取為組件,但是依舊有多個(gè)顯隱變量需要操控,可想而知一個(gè)單頁面文件會(huì)有多么龐大?。?!
所以,我們可以將常用的彈窗組件封裝后進(jìn)行函數(shù)式調(diào)用,函數(shù)式調(diào)用的好處有以下幾點(diǎn)
無需全局加載,需要使用的地方引入方法即可
無需定義顯示隱藏變量,關(guān)閉組件時(shí)會(huì)自動(dòng)銷毀組件
無需擔(dān)心數(shù)據(jù)留存銷毀問題,關(guān)閉時(shí)會(huì)隨組件一起銷毀
單頁面文件代碼量下降,只需引入組件并在彈窗組件使用即可
更多優(yōu)點(diǎn)繼續(xù)探索中...
效果預(yù)覽
技術(shù)實(shí)現(xiàn)
定義彈窗組件文件夾,如果想要使用ts,請(qǐng)關(guān)注vue3自定義彈窗
- Dialog
- dialog.js
- component.vue
定義component.vue,在編寫時(shí),曾考慮是使用slot插槽還是使用component組件,有這么一段話供各位參考
Vue.js 是一款流行的 JavaScript 框架,它提供了多種方式來組織和構(gòu)建您的應(yīng)用程序。其中兩個(gè)主要的方式是使用插槽(slots)和組件(components)。
插槽是 Vue.js 中一種非常強(qiáng)大的功能,它允許您在父組件中預(yù)留一些位置,然后在子組件中填充內(nèi)容。這種方式可以很方便地實(shí)現(xiàn)動(dòng)態(tài)組件和復(fù)雜布局。
組件是 Vue.js 中另一個(gè)核心概念,它允許您將應(yīng)用程序拆分為更小、更易于維護(hù)的部分。組件是自包含的,可以擁有自己的狀態(tài)、方法和生命周期鉤子。
在選擇使用插槽還是組件時(shí),需要根據(jù)您的具體需求來決定。如果您的應(yīng)用程序需要?jiǎng)討B(tài)布局和靈活性,那么插槽可能是更好的選擇。如果您的應(yīng)用程序需要更好的可維護(hù)性和可復(fù)用性,那么組件可能是更好的選擇。
所以最后還是考慮使用component編寫,不為別的,就是好玩
<!-- el-dialog為elementui 2.5版本的組件,尚無拖拽彈窗功能,如需在vue2中添加拖拽功能,可以查看其他文檔 --> <!-- 測(cè)試后,此處el-dialog可以替換為antd或其他UI庫(kù)dialog彈窗,同時(shí)也可以自己編寫彈窗樣式,皆可以滿足調(diào)用 --> <template> <el-dialog append-to-body :title="title" :visible.sync="visible" v-bind="dialogOption"> <!-- props傳入到組件內(nèi)數(shù)據(jù),接受即可 --> <component :is="component" v-bind="props" @cancel="cancel" @confirm="confirm"></component> </el-dialog> </template>
let resolve = null export default { data() { return { props: {}, title: '', dialogOption: {}, component: null, visible: true, onClose: () => { }, } }, methods: { //還可以更加精簡(jiǎn),但是目前為了方便易懂,先如此處理 open({ title, dialogOption, component, props, onClose }) { this.title = title this.dialogOption = dialogOption this.component = component this.props = props this.visible = true this.onClose = onClose //異步處理相關(guān)邏輯,未來可用 return new Promise((resolse, reject) => { resolve = resolse }) }, confirm(arg) { this.close() resolve(arg) }, cancel() { this.close() resolve() }, close() { //調(diào)用回調(diào)close,回傳傳入的close方法,銷毀組件 this.visible = false this.$emit('close') } }, };
定義好組件后,需要定義調(diào)用組件的dialog.js方法
import Component from './component.vue' import Vue from 'vue' //定義調(diào)用類 class Dialog { //定義相關(guān)實(shí)例,每次new Dialog的時(shí)候都會(huì)創(chuàng)建一個(gè)新的實(shí)例,這樣彈窗之間就不會(huì)相互影響 instance = null; component = null open(option) { //創(chuàng)建Vue實(shí)例,傳入相關(guān)參數(shù)信息 this.instance = new Vue({ render(h) { return h(Component, { title: option.title }, option.chidren) } }) //調(diào)用實(shí)例的$mount()掛載組件 this.component = this.instance.$mount(); document.body.appendChild(this.component.$el); const notification = this.instance.$children[0]; return notification.open(option); } close() { //傳入的銷毀方法,直接在dom樹上將當(dāng)前組件直接銷毀 if (this.component) { document.body.removeChild(this.component.$el) } this.component = null } } export default Dialog
具體實(shí)現(xiàn)時(shí),在使用彈窗的界面導(dǎo)入函數(shù)式彈窗組件及封裝的數(shù)據(jù)展示彈窗,將有關(guān)參數(shù)傳入即可
<template> <div> <p><el-button type="primary" @click="dialogDynamticFunc">測(cè)試按鈕</el-button></p> </div> </template> <script> //引入封裝組件及全局彈窗組件 import ChildComponent from './ChildComponent.vue' import Dialog from './Dialog/dialog' export default{ methods:{ dialogDynamticFunc() { let vueIns = this //new一個(gè)全局類 let win = new Dialog() //調(diào)用open方法,傳入相關(guān)參數(shù) win.open({ title: '選擇用戶', dialogOption: { width: '600px' }, component: ChildComponent, props: { id: 1 }, //定義回調(diào)關(guān)閉函數(shù), onClose: () => { win.close() } }) } } } </script>
以上就是Vue2+Elementui Dialog實(shí)現(xiàn)封裝自定義彈窗組件的詳細(xì)內(nèi)容,更多關(guān)于Vue自定義彈窗的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue3從0搭建Monorepo項(xiàng)目組件庫(kù)
這篇文章主要為大家介紹了Vue3從0搭建Monorepo項(xiàng)目組件庫(kù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02vue.js數(shù)據(jù)加載完成前顯示原代碼{{代碼}}問題及解決
這篇文章主要介紹了vue.js數(shù)據(jù)加載完成前顯示原代碼{{代碼}}問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Vue el-autocomplete遠(yuǎn)程搜索下拉框并實(shí)現(xiàn)自動(dòng)填充功能(推薦)
在elementui Input輸入框中可以找到遠(yuǎn)程搜索組件,獲取服務(wù)端的數(shù)據(jù)。這篇文章主要給大家介紹Vue el-autocomplete遠(yuǎn)程搜索下拉框并實(shí)現(xiàn)自動(dòng)填充功能,感興趣的朋友一起看看吧2019-10-10vue3中如何通過ref和$parent結(jié)合defineExpose實(shí)現(xiàn)父子組件之間的通信
這篇文章主要介紹了vue3中通過ref和$parent結(jié)合defineExpose實(shí)現(xiàn)父子組件之間的通信,Vue3中通過ref和$parent的結(jié)合使用,及defineExpose的方法,可以非常便捷地實(shí)現(xiàn)父子組件之間的通信,需要的朋友可以參考下2023-07-07使用Element時(shí)默認(rèn)勾選表格toggleRowSelection方式
這篇文章主要介紹了使用Element時(shí)默認(rèn)勾選表格toggleRowSelection方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10深入分析element ScrollBar滾動(dòng)組件源碼
這篇文章主要介紹了element ScrollBar滾動(dòng)組件源碼深入分析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01