vue.js開(kāi)發(fā)實(shí)現(xiàn)全局調(diào)用的MessageBox組件實(shí)例代碼
前言
一開(kāi)始接觸到vue中的組件的時(shí)候,對(duì)于組件的理解還是不夠充分的,最近在開(kāi)發(fā)個(gè)人博客項(xiàng)目中,一開(kāi)始就沒(méi)準(zhǔn)備使用一些現(xiàn)在比較流行的UI庫(kù)(畢竟是個(gè)人項(xiàng)目,多練練手還是好的),所以需要自己開(kāi)發(fā)幾個(gè)全局組件,這里以MessageBox為例記錄下vue.js如何開(kāi)發(fā)全局組件。所謂全局變量是針對(duì)vue實(shí)例下說(shuō)的,即所有的vue實(shí)際都可以運(yùn)用到這個(gè)組件,局部組件就是針對(duì)某個(gè)實(shí)例來(lái)說(shuō)的,只有這個(gè)vue實(shí)例下才能發(fā)揮作用,下面話不多說(shuō)了,來(lái)一看看詳細(xì)的介紹吧。
源碼
github地址:Talk is cheap. Show me the code.
本地下載地址:http://xiazai.jb51.net/201711/yuanma/vue-messagebox(jb51.net).rar
組件模板
// /src/components/MessageBox/index.vue
<template>
<div class="message-box" v-show="isShowMessageBox">
<div class="mask" @click="cancel"></div>
<div class="message-content">
<svg class="icon" aria-hidden="true" @click="cancel">
<use xlink:href="#icon-delete" rel="external nofollow" ></use>
</svg>
<h3 class="title">{{ title }}</h3>
<p class="content">{{ content }}</p>
<div>
<input type="text" v-model="inputValue" v-if="isShowInput" ref="input">
</div>
<div class="btn-group">
<button class="btn-default" @click="cancel" v-show="isShowCancelBtn">{{ cancelBtnText }}</button>
<button class="btn-primary btn-confirm" @click="confirm" v-show="isShowConfimrBtn">{{ confirmBtnText }}</button>
</div>
</div>
</div>
</template>
<script>
export default {
props: {
title: {
type: String,
default: '標(biāo)題'
},
content: {
type: String,
default: '這是彈框內(nèi)容'
},
isShowInput: false,
inputValue: '',
isShowCancelBtn: {
type: Boolean,
default: true
},
isShowConfimrBtn: {
type: Boolean,
default: true
},
cancelBtnText: {
type: String,
default: '取消'
},
confirmBtnText: {
type: String,
default: '確定'
}
},
data () {
return {
isShowMessageBox: false,
resolve: '',
reject: '',
promise: '' // 保存promise對(duì)象
};
},
methods: {
// 確定,將promise斷定為resolve狀態(tài)
confirm: function () {
this.isShowMessageBox = false;
if (this.isShowInput) {
this.resolve(this.inputValue);
} else {
this.resolve('confirm');
}
this.remove();
},
// 取消,將promise斷定為reject狀態(tài)
cancel: function () {
this.isShowMessageBox = false;
this.reject('cancel');
this.remove();
},
// 彈出messageBox,并創(chuàng)建promise對(duì)象
showMsgBox: function () {
this.isShowMessageBox = true;
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
// 返回promise對(duì)象
return this.promise;
},
remove: function () {
setTimeout(() => {
this.destroy();
}, 300);
},
destroy: function () {
this.$destroy();
document.body.removeChild(this.$el);
}
}
};
</script>
<style lang="scss" scoped>
// 此處省略 ...
</style>
給組件添加全局功能
vue.js官方文檔中有開(kāi)發(fā)插件的介紹。具體實(shí)現(xiàn)代碼如下:
// /src/components/MessageBox/index.js
import msgboxVue from './index.vue';
// 定義插件對(duì)象
const MessageBox = {};
// vue的install方法,用于定義vue插件
MessageBox.install = function (Vue, options) {
const MessageBoxInstance = Vue.extend(msgboxVue);
let currentMsg, instance;
const initInstance = () => {
// 實(shí)例化vue實(shí)例
currentMsg = new MessageBoxInstance();
let msgBoxEl = currentMsg.$mount().$el;
document.body.appendChild(msgBoxEl);
};
// 在Vue的原型上添加實(shí)例方法,以全局調(diào)用
Vue.prototype.$msgBox = {
showMsgBox (options) {
if (!instance) {
initInstance();
}
if (typeof options === 'string') {
currentMsg.content = options;
} else if (typeof options === 'object') {
Object.assign(currentMsg, options);
}
return currentMsg.showMsgBox();
}
};
};
export default MessageBox;
全局使用
// src/main.js import MessageBox from './components/MessageBox/index'; Vue.use(MessageBox);
頁(yè)面調(diào)用
按照之前定義好的方法,可以在各個(gè)頁(yè)面中愉快的調(diào)用該組件了。
this.$msgBox.showMsgBox({
title: '添加分類',
content: '請(qǐng)?zhí)顚懛诸惷Q',
isShowInput: true
}).then(async (val) => {
// ...
}).catch(() => {
// ...
});
最后來(lái)張效果圖

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
vue指令?v-bind的使用和注意需要注意的點(diǎn)
這篇文章主要給大家分享了?v-bind的使用和注意需要注意的點(diǎn),下面文章圍繞?v-bind指令的相關(guān)資料展開(kāi)內(nèi)容且附上詳細(xì)代碼?需要的小伙伴可以參考一下,希望對(duì)大家有所幫助2021-11-11
ant design vue 表格table 默認(rèn)勾選幾項(xiàng)的操作
這篇文章主要介紹了ant design vue 表格table 默認(rèn)勾選幾項(xiàng)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
vue中實(shí)現(xiàn)拖動(dòng)調(diào)整左右兩側(cè)div的寬度的示例代碼
這篇文章主要介紹了vue中實(shí)現(xiàn)拖動(dòng)調(diào)整左右兩側(cè)div的寬度的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue點(diǎn)擊按鈕實(shí)現(xiàn)讓頁(yè)面的某一個(gè)元素全屏展示
這篇文章主要介紹了vue點(diǎn)擊按鈕實(shí)現(xiàn)讓頁(yè)面的某一個(gè)元素全屏展示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue實(shí)現(xiàn)簡(jiǎn)易圖片左右旋轉(zhuǎn),上一張,下一張組件案例
這篇文章主要介紹了vue實(shí)現(xiàn)簡(jiǎn)易圖片左右旋轉(zhuǎn),上一張,下一張組件案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue3的setup在el-tab中動(dòng)態(tài)加載組件的方法
公司某項(xiàng)目需求在頁(yè)面顯示的組件是根據(jù)角色變化而變化的,怎么實(shí)現(xiàn)這個(gè)效果呢,下面小編給大家介紹下Vue3的setup在el-tab中動(dòng)態(tài)加載組件的方法,需要的朋友可以參考下2022-11-11
詳解Vue組件實(shí)現(xiàn)tips的總結(jié)
這篇文章主要介紹了詳解Vue組件實(shí)現(xiàn)tips的總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11

