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庫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: {
//還可以更加精簡,但是目前為了方便易懂,先如此處理
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)文章
vue項(xiàng)目中的webpack-dev-sever配置方法
下面小編就為大家分享一篇vue項(xiàng)目中的webpack-dev-sever配置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
ant design vue 表格table 默認(rèn)勾選幾項(xiàng)的操作
這篇文章主要介紹了ant design vue 表格table 默認(rèn)勾選幾項(xiàng)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue實(shí)現(xiàn)登錄時(shí)圖形驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)登錄時(shí)圖形驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Vue實(shí)現(xiàn)PopupWindow組件詳解
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)PopupWindow組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
vue自定v-model實(shí)現(xiàn)表單數(shù)據(jù)雙向綁定問題
vue.js的一大功能便是實(shí)現(xiàn)數(shù)據(jù)的雙向綁定。這篇文章主要介紹了vue自定v-model實(shí)現(xiàn) 表單數(shù)據(jù)雙向綁定的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
解決vue bus.$emit觸發(fā)第一次$on監(jiān)聽不到問題
這篇文章主要介紹了解決vue bus.$emit觸發(fā)第一次$on監(jiān)聽不到問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
一文詳解如何解決Vue2前端項(xiàng)目無法訪問本地資源問題
最近遇到不少同學(xué)反饋,Vue2 項(xiàng)目能 npm run serve 啟動(dòng),但頁面一直轉(zhuǎn)圈,某個(gè) chunk.js 加載不到(pending),本文我們就來看看具體解決方法吧2025-09-09

