vue.js中怎么全局調用MessageBox組件-創新互聯

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組件

關于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。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站優化排名
主站蜘蛛池模板: 日韩少妇无码喷潮系列一二三| 蜜桃无码一区二区三区| 久久亚洲精品成人av无码网站| 亚洲AV无码一区东京热| 国产成人无码A区精油按摩| 亚洲国产成人片在线观看无码| 亚洲欧洲免费无码| 亚洲成AV人在线观看天堂无码| WWW久久无码天堂MV| 无码国产色欲XXXX视频| 午夜成人无码福利免费视频| 成人免费午夜无码视频| 色欲A∨无码蜜臀AV免费播 | 国产白丝无码免费视频| 成人免费无码视频在线网站| 久久水蜜桃亚洲av无码精品麻豆| 大桥久未无码吹潮在线观看| 人妻丰满熟妇A v无码区不卡| 99国产精品无码| 精品亚洲AV无码一区二区三区 | 在线看片福利无码网址| 亚洲av成人无码久久精品| 中文字幕乱妇无码AV在线| 精品国产毛片一区二区无码| 国产精品无码一区二区在线| 日本精品无码一区二区三区久久久 | 欧洲人妻丰满av无码久久不卡 | 国产成人精品无码免费看| 亚洲精品无码你懂的网站| 亚洲成A人片在线观看无码3D | 亚洲综合最新无码专区| 永久免费无码网站在线观看个| 性色AV无码中文AV有码VR| 在线A级毛片无码免费真人| 精品人妻中文无码AV在线| 四虎成人精品国产永久免费无码 | 日韩放荡少妇无码视频| 国产午夜无码片在线观看影院 | 无码精品一区二区三区免费视频 | 无码AV中文一区二区三区| 无码人妻品一区二区三区精99 |