vue.js中怎么全局調用MessageBox組件,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創新互聯建站是一家專業提供長子企業網站建設,專注與網站設計、網站建設、HTML5建站、小程序制作等業務。10年已為長子眾多企業、政府機構等服務。創新互聯專業網站制作公司優惠進行中。組件模板
// /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> <h4 class="title">{{ title }}</h4> <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: '標題' }, content: { type: String, default: '這是彈框內容' }, 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狀態 confirm: function () { this.isShowMessageBox = false; if (this.isShowInput) { this.resolve(this.inputValue); } else { this.resolve('confirm'); } this.remove(); }, // 取消,將promise斷定為reject狀態 cancel: function () { this.isShowMessageBox = false; this.reject('cancel'); this.remove(); }, // 彈出messageBox,并創建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官方文檔中有開發插件的介紹。具體實現代碼如下:
// /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 = () => { // 實例化vue實例 currentMsg = new MessageBoxInstance(); let msgBoxEl = currentMsg.$mount().$el; document.body.appendChild(msgBoxEl); }; // 在Vue的原型上添加實例方法,以全局調用 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);
頁面調用
按照之前定義好的方法,可以在各個頁面中愉快的調用該組件了。
this.$msgBox.showMsgBox({ title: '添加分類', content: '請填寫分類名稱', isShowInput: true }).then(async (val) => { // ... }).catch(() => { // ... });
最后來張效果圖
關于vue.js中怎么全局調用MessageBox組件問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創新互聯成都網站設計公司行業資訊頻道了解更多相關知識。
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網頁標題:vue.js中怎么全局調用MessageBox組件-創新互聯
標題路徑:http://m.kartarina.com/article36/ccggsg.html
成都網站建設公司_創新互聯,為您提供面包屑導航、網站維護、網頁設計公司、App開發、搜索引擎優化、網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯