vue.js開發(fā)實(shí)現(xiàn)全局調(diào)用的MessageBox組件實(shí)例代碼
前言
一開始接觸到vue中的組件的時候,對于組件的理解還是不夠充分的,最近在開發(fā)個人博客項(xiàng)目中,一開始就沒準(zhǔn)備使用一些現(xiàn)在比較流行的UI庫(畢竟是個人項(xiàng)目,多練練手還是好的),所以需要自己開發(fā)幾個全局組件,這里以MessageBox為例記錄下vue.js如何開發(fā)全局組件。所謂全局變量是針對vue實(shí)例下說的,即所有的vue實(shí)際都可以運(yùn)用到這個組件,局部組件就是針對某個實(shí)例來說的,只有這個vue實(shí)例下才能發(fā)揮作用,下面話不多說了,來一看看詳細(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對象 }; }, 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對象 showMsgBox: function () { this.isShowMessageBox = true; this.promise = new Promise((resolve, reject) => { this.resolve = resolve; this.reject = reject; }); // 返回promise對象 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官方文檔中有開發(fā)插件的介紹。具體實(shí)現(xiàn)代碼如下:
// /src/components/MessageBox/index.js import msgboxVue from './index.vue'; // 定義插件對象 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);
頁面調(diào)用
按照之前定義好的方法,可以在各個頁面中愉快的調(diào)用該組件了。
this.$msgBox.showMsgBox({ title: '添加分類', content: '請?zhí)顚懛诸惷Q', isShowInput: true }).then(async (val) => { // ... }).catch(() => { // ... });
最后來張效果圖
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Element?table?上下移需求的實(shí)現(xiàn)
本文主要介紹了Element?table?上下移需求的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07vue?el-date-picker?日期回顯后無法改變問題解決
這篇文章主要介紹了vue?el-date-picker?日期回顯后無法改變問題解決,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04Vue如何將當(dāng)前窗口截圖并將數(shù)據(jù)base64轉(zhuǎn)為png格式傳給服務(wù)器
這篇文章主要介紹了Vue如何將當(dāng)前窗口截圖并將數(shù)據(jù)base64轉(zhuǎn)為png格式傳給服務(wù)器,通過實(shí)例代碼介紹了將當(dāng)前窗口截圖,并將數(shù)據(jù)存儲下來,需要的朋友可以參考下2023-10-10Vue3+TypeScript實(shí)現(xiàn)Docx/Excel預(yù)覽組件
這篇文章主要為大家詳細(xì)介紹了如何使用Vue3+TypeScript實(shí)現(xiàn)Docx/Excel預(yù)覽組件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2024-04-04