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 和組件插槽來實(shí)現(xiàn)彈框內(nèi)容的展示,并添加了一些我們需要的屬性和方法。 然后定義了一個(gè) ModalConstructor 構(gòu)造函數(shù),通過 show 方法來打開彈框,并返回 Promise 來等待用戶的操作,最后返回用戶的操作結(jié)果。
在 show 方法中,我們通過傳入組件和 props 的方式來動(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)的折疊展開實(shí)現(xiàn)
- 詳解GoJs節(jié)點(diǎn)的選中高亮實(shí)現(xiàn)示例
- go.js的基本使用方法詳解【與vue,react同理】
相關(guān)文章
解決Vue.js由于延時(shí)顯示了{(lán){message}}引用界面的問題
今天小編就為大家分享一篇解決Vue.js由于延時(shí)顯示了{(lán){message}}引用界面的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
如何在Vue3和Vite項(xiàng)目中用SQLite數(shù)據(jù)庫進(jìn)行數(shù)據(jù)存儲(chǔ)
SQLite是一種嵌入式關(guān)系型數(shù)據(jù)庫管理系統(tǒng),是一個(gè)零配置、無服務(wù)器的、自給自足的、事務(wù)性的SQL數(shù)據(jù)庫引擎,這篇文章主要給大家介紹了關(guān)于如何在Vue3和Vite項(xiàng)目中用SQLite數(shù)據(jù)庫進(jìn)行數(shù)據(jù)存儲(chǔ)的相關(guān)資料,需要的朋友可以參考下2024-03-03
vue.js 實(shí)現(xiàn)a標(biāo)簽href里添加參數(shù)
今天小編就為大家分享一篇vue.js 實(shí)現(xiàn)a標(biāo)簽href里添加參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue中使用console.log打印的實(shí)現(xiàn)
這篇文章主要介紹了vue中使用console.log打印的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue項(xiàng)目啟動(dòng)時(shí)的端口占用問題分析與解決方案
你有沒有遇到過這種情況:當(dāng)你滿懷期待地輸入 npm run serve,準(zhǔn)備啟動(dòng) Vue 項(xiàng)目時(shí),突然蹦出一堆紅色錯(cuò)誤信息,其中最顯眼的就是 EADDRINUSE?本文我們就來深入分析這個(gè)問題,并手把手教你如何解決它,需要的朋友可以參考下2025-09-09
sockjs-node/info跨域請(qǐng)求問題及解決過程
這篇文章主要介紹了sockjs-node/info跨域請(qǐng)求問題及解決過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2026-03-03

