Vue+Element實(shí)現(xiàn)封裝抽屜彈框
一進(jìn)行showDrawer()封裝
新建dialog.js
// 彈框模板 import Vue from 'vue'; import { Drawer } from 'element-ui'; const modalTemplate = ` <el-drawer :visible.sync="visible" :title="title" :size="size" :direction="direction"> <component :is="component" v-bind="props" @ok="handleOk" @cancel="handleCancel"></component> </el-drawer> ` // 彈框構(gòu)造函數(shù) const ModalConstructor = Vue.extend({ template: modalTemplate, data() { return { visible: false, title: '', size: '70%', direction: 'rtl', component: null, props: null, resolve: null, reject: null, } }, methods: { show(component, props, options) { this.visible = true this.component = component this.props = props || {} this.title = options.title || '提示' this.size = options.width || '70%' this.direction = options.direction || 'rtl' return new Promise((resolve, reject) => { this.resolve = resolve this.reject = reject }) }, hide() { this.visible = false }, handleOk(res) { this.resolve(res) this.hide() }, handleCancel() { this.reject() this.hide() }, }, }) // 創(chuàng)建組件實(shí)例并掛載到文檔中 const Modal = new ModalConstructor().$mount() document.body.appendChild(Modal.$el) // $showDrawer 方法 Vue.prototype.$showDrawer = function (component, props, options) { return Modal.show(component, props, options) } // $showDialog 方法 Vue.prototype.$showDialog = function (component, props, options) { return new Promise((resolve, reject) => { let MyDialogConstructor = Vue.extend({ components: { 'my-dialog': component }, template: `<el-dialog :visible.sync="visible" :title="title" :width="width" :before-close="beforeClose"> <my-dialog :props="props" @ok="handleOk" @cancel="handleCancel"></my-dialog> </el-dialog>`, data() { return { visible: true, title: options.title || '提示', width: options.width || '50%', props: props, resolve: resolve, reject: reject, } }, methods: { hide() { this.visible = false }, handleOk(res) { this.resolve(res) this.hide() }, handleCancel() { this.reject() this.hide() }, beforeClose(done) { ElementUI.MessageBox.confirm('確認(rèn)關(guān)閉?').then(() => { done() }).catch(() => {}) }, }, }) let MyDialog = new MyDialogConstructor().$mount() document.body.appendChild(MyDialog.$el) }) }
在上面的代碼中,我們首先定義了一個(gè) modalTemplate
模板,使用 Element UI 的 Drawer 和組件插槽來(lái)實(shí)現(xiàn)彈框內(nèi)容的展示,并添加了一些我們需要的屬性和方法。 然后定義了一個(gè) ModalConstructor 構(gòu)造函數(shù),通過(guò) show
方法來(lái)打開(kāi)彈框,并返回 Promise 來(lái)等待用戶的操作,最后返回用戶的操作結(jié)果。
在 show
方法中,我們通過(guò)傳入組件和 props 的方式來(lái)動(dòng)態(tài)渲染組件,并設(shè)置其他選項(xiàng),如彈框的標(biāo)題、大小、方向等。
## 2在main.js里引入dialog.js ```js import '@util/dialog'
如何使用
// 調(diào)用 $showDrawer 方法 this.$showDrawer(ExamDetail, { examId: rowId }, { title: '考試詳情', width: '1200px', }).then(() => { this.searchForm() }) // 調(diào)用 $showDialog 方法 this.$showDialog(ExamDetail, { examId: rowId }, { title: '考試詳情', width: '1200px', }).then(() => { this.searchForm() })
到此這篇關(guān)于Vue+Element實(shí)現(xiàn)封裝抽屜彈框的文章就介紹到這了,更多相關(guān)Vue Element抽屜彈框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue3 Suspense處理異步組件加載的工作原理
- ant-design-vue按鈕樣式擴(kuò)展方法詳解
- Vue多重文字描邊組件實(shí)現(xiàn)示例詳解
- vue實(shí)現(xiàn)高德地圖添加多個(gè)點(diǎn)標(biāo)記
- vue element-ui表格自定義動(dòng)態(tài)列具體實(shí)現(xiàn)
- vue3+element 分片上傳與分片下載功能實(shí)現(xiàn)方法詳解
- 淺析Vue如何巧用computed計(jì)算屬性
- GoJs中標(biāo)題和縮略圖使用技巧
- 詳解GoJs節(jié)點(diǎn)的折疊展開(kāi)實(shí)現(xiàn)
- 詳解GoJs節(jié)點(diǎn)的選中高亮實(shí)現(xiàn)示例
- go.js的基本使用方法詳解【與vue,react同理】
相關(guān)文章
Vue時(shí)間軸 vue-light-timeline的用法說(shuō)明
這篇文章主要介紹了Vue時(shí)間軸 vue-light-timeline的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10Vue3使用MD5加密實(shí)戰(zhàn)案例(清晰明了)
MD5是一種信息摘要算法(對(duì)稱加密),一種被廣泛使用的密碼散列函數(shù),可以產(chǎn)生出一個(gè)128位(16字節(jié))的散列值,用來(lái)確保信息傳輸完整一致性,這篇文章主要給大家介紹了關(guān)于Vue3使用MD5加密的相關(guān)資料,需要的朋友可以參考下2023-05-05vue如何調(diào)用攝像頭實(shí)現(xiàn)拍照上傳圖片、本地上傳圖片
這篇文章主要給大家介紹了關(guān)于vue如何調(diào)用攝像頭實(shí)現(xiàn)拍照上傳圖片、本地上傳圖片的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07vue實(shí)現(xiàn)輸入框自動(dòng)跳轉(zhuǎn)功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)輸入框自動(dòng)跳轉(zhuǎn)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05Vue3.2?setup語(yǔ)法糖及Hook函數(shù)基本使用
這篇文章主要為大家介紹了Vue3.2?setup語(yǔ)法糖及Hook函數(shù)基本使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07詳解vue axios用post提交的數(shù)據(jù)格式
這篇文章主要介紹了詳解vue axios用post提交的數(shù)據(jù)格式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08vue form表單使用resetFields函數(shù)出現(xiàn)的問(wèn)題
這篇文章主要介紹了vue form表單使用resetFields函數(shù)出現(xiàn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05vue構(gòu)建單頁(yè)面應(yīng)用實(shí)戰(zhàn)
本篇文章主要介紹了vue構(gòu)建單頁(yè)面應(yīng)用實(shí)戰(zhàn),使用 SPA,沒(méi)有頁(yè)面切換,就沒(méi)有白屏阻塞,可以大大提高 H5 的性能,達(dá)到接近原生的流暢體驗(yàn)。2017-04-04