這篇文章將為大家詳細(xì)講解有關(guān)Vue中Toast的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

一、效果圖

二、說(shuō)明
這類(lèi)提示框組件我們通常都會(huì)直接在 JS 代碼中進(jìn)行調(diào)用。像下面這樣:
this.$toast('別點(diǎn)啦,到頭啦!')但看到網(wǎng)上大多數(shù)還是通過(guò) component 方式實(shí)現(xiàn)的,這樣的話我們?cè)谑褂玫臅r(shí)候還要在 DOM 中放置一個(gè)組件元素,然后通過(guò)一個(gè)變量來(lái)控制它的顯示隱藏,這樣使用起來(lái)非常的不方便。那么有什么方法可以不用在 DOM 中手動(dòng)放置組件元素就可以直接調(diào)用呢?答案就是 Vue.extend。通過(guò) Vue.extend 方式實(shí)現(xiàn)的組件,需要兩個(gè)文件,一個(gè)是 .vue 文件,另外一個(gè)就是管理 .vue 的 .js 文件。具體代碼如下:
三、代碼
Toast.vue 文件代碼
<template>
<div class="toast" v-show="visible">
{{ message }}
</div>
</template>
<script>
export default {
name: 'toast',
data () {
return {
message: '', // 消息文字
duration: 3000, // 顯示時(shí)長(zhǎng),毫秒
closed: false, // 用來(lái)判斷消息框是否關(guān)閉
timer: null, // 計(jì)時(shí)器
visible: false // 是否顯示
}
},
mounted () {
this.startTimer()
},
watch: {
closed (newVal) {
if (newVal) {
this.visible = false
this.destroyElement()
}
}
},
methods: {
destroyElement () {
this.$destroy()
this.$el.parentNode.removeChild(this.$el)
},
startTimer () {
this.timer = setTimeout(() => {
if (!this.closed) {
this.closed = true
clearTimeout(this.timer)
}
}, this.duration)
}
}
}
</script>
<style lang="scss" scoped>
.toast {
position: fixed;
bottom: 15vh;
left: 28vw;
min-width: 40vw;
max-width: 100vw;
font-size: 26px;
text-align: center;
padding: 10px 2vw;
border-radius: 40px;
background-color: rgba(0, 0, 0 , 0.6);
color: rgb(223, 219, 219);
letter-spacing: 3px;
}
</style>Toast.js 文件代碼
import Vue from 'vue'
import Toast from '@/components/layer/Toast.vue'
let ToastConstructor = Vue.extend(Toast) // 構(gòu)造函數(shù)
let instance // 實(shí)例對(duì)象
let seed = 1 // 計(jì)數(shù)
const ToastDialog = (options = {}) => {
if (typeof options === 'string') {
options = {
message: options
}
}
let id = `toast_${seed++}`
instance = new ToastConstructor({
data: options
})
instance.id = id
instance.vm = instance.$mount()
document.body.appendChild(instance.vm.$el)
instance.vm.visible = true
instance.dom = instance.vm.$el
instance.dom.style.zIndex = 999 + seed
return instance.vm
}
export default ToastDialog四、使用
首先在 main.js 中引入 Toast.js 并掛載到vue原型上,如下圖:

其次,在代碼中使用
this.$toast('別點(diǎn)啦,到頭啦!')關(guān)于“Vue中Toast的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
本文標(biāo)題:Vue中Toast的示例分析-創(chuàng)新互聯(lián)
文章源于:http://www.chinadenli.net/article6/dcceig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、虛擬主機(jī)、網(wǎng)站設(shè)計(jì)、響應(yīng)式網(wǎng)站、Google、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容