前言
一開(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í)顚?xiě)分類名稱', isShowInput: true }).then(async (val) => { // ... }).catch(() => { // ... });
最后來(lái)張效果圖
總結(jié)
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com