本文實例為大家分享了vue toast彈框全局調用示例,供大家參考,具體內容如下
1.首選新建一個toast.vue模板文件:
 <template> <transition :name="fadeIn">  <div class="alertBox" v-show="show">   <div class="alert-mask" v-show="isShowMask"></div>   <transition :name="translate">    <div class="box" :class="position" v-show="show">     {{text}}    </div>   </transition>  </div> </transition></template><script>export default { data() {  return {  } }, props: {  show: { // 是否顯示此toast   default: false  },  text: { // 提醒文字   default: 'loading'  },  position: { // 提醒容器位置   default: 'center'  },  isShowMask: { // 是否顯示遮罩層   default: false  },  time: { // 顯示時間   default: 1500  },  transition: { // 是否開啟動畫   default: true  } }, mounted() { // 時間控制  setTimeout(() => {   this.show = false  }, this.time) }, computed: {  translate() { // 根據props,生成相對應的動畫   if (!this.transition) {    return ''   } else {    if (this.position === 'top') {     return 'translate-top'    } else if (this.position === 'middle') {     return 'translate-middle'    } else if (this.position === 'bottom') {     return 'translate-bottom'    }   }  },  fadeIn() { // 同上   if (!this.transition) {    return ''   } else {    return 'fadeIn'   }  } }}</script><style> .box{  position: fixed;  top: 50%;  left: 50%;  width: 100px;  height: 100px;  margin-left: -50px;  margin-top: -50px;  background: rgba(0,0,0,.5);  text-align: center;  line-height: 100px;  color: #fff;  font-size: 16px;  z-index: 5000;  color: #fff; } .box.top{  top: 50px;  margin-top: 0; } .box.center{  top: 50%;  margin-top: -100px; } .box.bottom{  top: auto;  bottom: 50px;  margin-top: 0; } .alert-mask{  position: fixed;  left: 0;  top: 0;  bottom: 0;  right: 0;  background: rgba(0,0,0,.5);  z-index: 4999; } .fadeIn-enter-active, .fadeIn-leave-active{  transition: opacity .3s; } .fadeIn-enter, .fadeIn-leave-active{  opacity: 0; } .translate-top-enter-active, .translate-top-leave-active{  transition: all 0.3s cubic-bezier(.36,.66,.04,1); } .translate-top-enter, .translate-top-leave-active{  transform: translateY(-50%);  opacity: 0; } .translate-middle-enter-active, .translate-middle-leave-active{  transition: all 0.3s cubic-bezier(.36,.66,.04,1); } .translate-middle-enter, .translate-middle-leave-active{  transform: translateY(80%);  opacity: 0; } .translate-bottom-enter-active, .translate-bottom-leave-active{  transition: all 0.3s cubic-bezier(.36,.66,.04,1); } .translate-bottom-enter, .translate-bottom-leave-active{  transform: translateY(100%);  opacity: 0; }</style>2.主邏輯在toast.js里完成:
var Alert = require('./index.vue') // 引入vue模板var Toast = {} // 定義插件對象Toast.install = function (Vue, options) { // vue的install方法,用于定義vue插件 // 如果toast還在,則不再執行 if(document.getElementsByClassName('alertBox').length){    return } let toastTpl = Vue.extend(Alert) // 創建vue構造器 // el:提供一個在頁面上已存在的DOM元素作為Vue實例的掛載目標。可以是css選擇器,也可以是HTMLElement實例。 // 在實例掛載之后,可以通過$vm.$el訪問。 // 如果這個選項在實例化時有用到,實例將立即進入編譯過程。否則,需要顯示調用vm.$mount()手動開啟編譯(如下) // 提供的元素只能作為掛載點。所有的掛載元素會被vue生成的dom替換。因此不能掛載在頂級元素(html, body)上 // let $vm = new toastTpl({ //  el: document.createElement('div') // }) let $vm = new toastTpl() // 實例化vue實例 // 此處使用$mount來手動開啟編譯。用$el來訪問元素,并插入到body中 let tpl = $vm.$mount().$el document.body.appendChild(tpl) Vue.prototype.$toast = { // 在Vue的原型上添加實例方法,以全局調用  show(options) { // 控制toast顯示的方法   if (typeof options === 'string') { // 對參數進行判斷    $vm.text = options // 傳入props   }   else if (typeof options === 'object') {    Object.assign($vm, options) // 合并參數與實例   }   $vm.show = true // 顯示toast  },  hide() { // 控制toast隱藏的方法   $vm.show = false  } }}export default Toast; // 導出Toast(注意:此處不能用module exports導出,在一個文件中,不能同時使用require方式引入,而用module exports導出,兩種方式不能混用)使用:
在vue項目的主文件中,引入插件,并進行安裝:

這樣在項目的任何組件里,都可以使用這個toast的彈窗插件了:

想要更高級的插件學習源碼,請移步vux進行源碼學習 https://vux.li/#/zh-CN/demos/toast
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答