Vue2+Elementui?Dialog實現(xiàn)封裝自定義彈窗組件
前言
在日常的管理系統(tǒng)界面中,我們寫的最多的除了列表表格之外,就是各種彈窗組件,如果將彈窗組件寫在上下文的話,每個頁面都需要編寫大量重復的dialog代碼已經(jīng)頻繁的創(chuàng)建控制顯示隱藏的變量信息,即使將彈窗內(nèi)展示信息抽取為組件,但是依舊有多個顯隱變量需要操控,可想而知一個單頁面文件會有多么龐大?。?!
所以,我們可以將常用的彈窗組件封裝后進行函數(shù)式調(diào)用,函數(shù)式調(diào)用的好處有以下幾點
無需全局加載,需要使用的地方引入方法即可
無需定義顯示隱藏變量,關閉組件時會自動銷毀組件
無需擔心數(shù)據(jù)留存銷毀問題,關閉時會隨組件一起銷毀
單頁面文件代碼量下降,只需引入組件并在彈窗組件使用即可
更多優(yōu)點繼續(xù)探索中...
效果預覽

技術實現(xiàn)
定義彈窗組件文件夾,如果想要使用ts,請關注vue3自定義彈窗
- Dialog
- dialog.js
- component.vue
定義component.vue,在編寫時,曾考慮是使用slot插槽還是使用component組件,有這么一段話供各位參考
Vue.js 是一款流行的 JavaScript 框架,它提供了多種方式來組織和構建您的應用程序。其中兩個主要的方式是使用插槽(slots)和組件(components)。
插槽是 Vue.js 中一種非常強大的功能,它允許您在父組件中預留一些位置,然后在子組件中填充內(nèi)容。這種方式可以很方便地實現(xiàn)動態(tài)組件和復雜布局。
組件是 Vue.js 中另一個核心概念,它允許您將應用程序拆分為更小、更易于維護的部分。組件是自包含的,可以擁有自己的狀態(tài)、方法和生命周期鉤子。
在選擇使用插槽還是組件時,需要根據(jù)您的具體需求來決定。如果您的應用程序需要動態(tài)布局和靈活性,那么插槽可能是更好的選擇。如果您的應用程序需要更好的可維護性和可復用性,那么組件可能是更好的選擇。
所以最后還是考慮使用component編寫,不為別的,就是好玩
<!-- el-dialog為elementui 2.5版本的組件,尚無拖拽彈窗功能,如需在vue2中添加拖拽功能,可以查看其他文檔 -->
<!-- 測試后,此處el-dialog可以替換為antd或其他UI庫dialog彈窗,同時也可以自己編寫彈窗樣式,皆可以滿足調(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
//異步處理相關邏輯,未來可用
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 {
//定義相關實例,每次new Dialog的時候都會創(chuàng)建一個新的實例,這樣彈窗之間就不會相互影響
instance = null;
component = null
open(option) {
//創(chuàng)建Vue實例,傳入相關參數(shù)信息
this.instance = new Vue({
render(h) {
return h(Component, { title: option.title }, option.chidren)
}
})
//調(diào)用實例的$mount()掛載組件
this.component = this.instance.$mount();
document.body.appendChild(this.component.$el);
const notification = this.instance.$children[0];
return notification.open(option);
}
close() {
//傳入的銷毀方法,直接在dom樹上將當前組件直接銷毀
if (this.component) {
document.body.removeChild(this.component.$el)
}
this.component = null
}
}
export default Dialog
具體實現(xiàn)時,在使用彈窗的界面導入函數(shù)式彈窗組件及封裝的數(shù)據(jù)展示彈窗,將有關參數(shù)傳入即可
<template>
<div>
<p><el-button type="primary" @click="dialogDynamticFunc">測試按鈕</el-button></p>
</div>
</template>
<script>
//引入封裝組件及全局彈窗組件
import ChildComponent from './ChildComponent.vue'
import Dialog from './Dialog/dialog'
export default{
methods:{
dialogDynamticFunc() {
let vueIns = this
//new一個全局類
let win = new Dialog()
//調(diào)用open方法,傳入相關參數(shù)
win.open({
title: '選擇用戶',
dialogOption: { width: '600px' },
component: ChildComponent,
props: {
id: 1
},
//定義回調(diào)關閉函數(shù),
onClose: () => {
win.close()
}
})
}
}
}
</script>
以上就是Vue2+Elementui Dialog實現(xiàn)封裝自定義彈窗組件的詳細內(nèi)容,更多關于Vue自定義彈窗的資料請關注腳本之家其它相關文章!
相關文章
vue.js數(shù)據(jù)加載完成前顯示原代碼{{代碼}}問題及解決
這篇文章主要介紹了vue.js數(shù)據(jù)加載完成前顯示原代碼{{代碼}}問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Vue el-autocomplete遠程搜索下拉框并實現(xiàn)自動填充功能(推薦)
在elementui Input輸入框中可以找到遠程搜索組件,獲取服務端的數(shù)據(jù)。這篇文章主要給大家介紹Vue el-autocomplete遠程搜索下拉框并實現(xiàn)自動填充功能,感興趣的朋友一起看看吧2019-10-10
vue3中如何通過ref和$parent結(jié)合defineExpose實現(xiàn)父子組件之間的通信
這篇文章主要介紹了vue3中通過ref和$parent結(jié)合defineExpose實現(xiàn)父子組件之間的通信,Vue3中通過ref和$parent的結(jié)合使用,及defineExpose的方法,可以非常便捷地實現(xiàn)父子組件之間的通信,需要的朋友可以參考下2023-07-07
使用Element時默認勾選表格toggleRowSelection方式
這篇文章主要介紹了使用Element時默認勾選表格toggleRowSelection方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

