vue教程之toast彈框全局調(diào)用示例詳解
本文實(shí)例為大家分享了vue toast彈框全局調(diào)用示例,供大家參考,具體內(nèi)容如下
1.首選新建一個(gè)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: { // 顯示時(shí)間
default: 1500
},
transition: { // 是否開啟動畫
default: true
}
},
mounted() { // 時(shí)間控制
setTimeout(() => {
this.show = false
}, this.time)
},
computed: {
translate() { // 根據(jù)props,生成相對應(yīng)的動畫
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還在,則不再執(zhí)行
if(document.getElementsByClassName('alertBox').length){
return
}
let toastTpl = Vue.extend(Alert) // 創(chuàng)建vue構(gòu)造器
// el:提供一個(gè)在頁面上已存在的DOM元素作為Vue實(shí)例的掛載目標(biāo)??梢允莄ss選擇器,也可以是HTMLElement實(shí)例。
// 在實(shí)例掛載之后,可以通過$vm.$el訪問。
// 如果這個(gè)選項(xiàng)在實(shí)例化時(shí)有用到,實(shí)例將立即進(jìn)入編譯過程。否則,需要顯示調(diào)用vm.$mount()手動開啟編譯(如下)
// 提供的元素只能作為掛載點(diǎn)。所有的掛載元素會被vue生成的dom替換。因此不能掛載在頂級元素(html, body)上
// let $vm = new toastTpl({
// el: document.createElement('div')
// })
let $vm = new toastTpl() // 實(shí)例化vue實(shí)例
// 此處使用$mount來手動開啟編譯。用$el來訪問元素,并插入到body中
let tpl = $vm.$mount().$el
document.body.appendChild(tpl)
Vue.prototype.$toast = { // 在Vue的原型上添加實(shí)例方法,以全局調(diào)用
show(options) { // 控制toast顯示的方法
if (typeof options === 'string') { // 對參數(shù)進(jìn)行判斷
$vm.text = options // 傳入props
}
else if (typeof options === 'object') {
Object.assign($vm, options) // 合并參數(shù)與實(shí)例
}
$vm.show = true // 顯示toast
},
hide() { // 控制toast隱藏的方法
$vm.show = false
}
}
}
export default Toast; // 導(dǎo)出Toast(注意:此處不能用module exports導(dǎo)出,在一個(gè)文件中,不能同時(shí)使用require方式引入,而用module exports導(dǎo)出,兩種方式不能混用)
使用:
在vue項(xiàng)目的主文件中,引入插件,并進(jìn)行安裝:

這樣在項(xiàng)目的任何組件里,都可以使用這個(gè)toast的彈窗插件了:

想要更高級的插件學(xué)習(xí)源碼,請移步vux進(jìn)行源碼學(xué)習(xí)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目fetch本地PAG格式文件404 NotFound的解決
這篇文章主要介紹了vue項(xiàng)目fetch本地PAG格式文件404 NotFound的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue3中安裝使用vue-i18n實(shí)時(shí)切換語言且不用刷新
這篇文章主要介紹了vue3中安裝使用vue-i18n實(shí)時(shí)切換語言不用刷新問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vuex state及mapState的基礎(chǔ)用法詳解
這篇文章主要介紹了vuex state及mapState的基礎(chǔ)用法詳解,本文通過實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-04-04
vue axios調(diào)用接口方法報(bào)錯(cuò)500 internal server err
前端使用axios 訪問后端接口時(shí)報(bào)錯(cuò),在瀏覽器中點(diǎn)擊紅色的報(bào)錯(cuò)數(shù)據(jù),本文給大家分享vue axios調(diào)用接口方法報(bào)錯(cuò)500 internal server error的兩種解決方法,感興趣的朋友一起看看吧2023-10-10
vue-electron使用serialport時(shí)問題解決方案
這篇文章主要介紹了vue-electron使用serialport時(shí)問題解決方案,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09

