寫了兩三天,終于把toast組件寫出來了。不敢說是最好的設(shè)計(jì),希望有更好思路的朋友可以在評(píng)論區(qū)給我意見!_(:з」∠)_
第一步:寫toast.vue,將樣式之類的先定下來
<template> <div v-show="showToast" class="toast" :class="position"> <div class="toast_container" v-if="type=='success'"> <div><i class="iconfont icon-check icon"></i></div> <div class="msg_container">{{message}}</div> </div> <div class="toast_container" v-else-if="type=='wrong'"> <div><i class="iconfont icon-warning-circle icon"></i></div> <div class="msg_container">{{message}}</div> </div> <div class="toast_container" v-else-if="type=='loading'"> <div><loading10></loading10></div> <div class="msg_container">{{message}}</div> </div> </div> </template> <script> import loading10 from '../loading/spiner' export default{ props:{ message:String, type:{ validator: function (value) { // 值必須是這些字符串中的一個(gè) return ['success', 'wrong', 'loading'].indexOf(value) !== -1 }, default:'success' }, duration:{ type:Number, default:3000 }, position:{ type:String, default:'middle' } }, components:{ loading10 }, data(){ return{ showToast:false } } } </script> <style scoped> .toast{ width:100%; } .toast_container{ background: rgba(0, 0, 0, 0.7); border-radius: 8px; color:#fff; margin-left:88px; margin-right:88px; text-align:center; padding-top:15px; padding-bottom: 15px; } .top{ position:absolute; top:10%; } .middle{ position:absolute; top:40%; } .bottom{ position:absolute; top:70%; } .msg_container{ margin-top:8px; margin-left:15px; margin-right:15px; line-height: 22px; font-size: 16px; word-wrap: break-word; } .icon{ font-size:30px; } </style>
一共三種樣式,成功(success),失?。╳rong),加載中(loading);
一共三種位置,上(top),中(middle),下(bottom);
所有涉及的圖案出自阿里的iconfont 手機(jī)淘寶圖標(biāo)庫。
加載中動(dòng)畫是自己寫的蹩腳的加載組件(emmm,就不放出來污染大家眼睛了,需要的可以評(píng)論區(qū)知會(huì)一聲_(:з」∠)_)
第二步:寫index.js ,完成toast組件的實(shí)例化
import Vue from 'vue' import Toast from './toast' let singleToast=true; let queue=[]; function createInstance(){ // 返回一個(gè)擴(kuò)展實(shí)例構(gòu)造器 if(!queue.length||!singleToast){ const ToastConstructor = Vue.extend(Toast); // 構(gòu)造一個(gè)實(shí)例 const toastDom = new ToastConstructor({ el: document.createElement('div'), }); // 把實(shí)例化的 toast.vue 添加到 body 里 document.body.appendChild(toastDom.$el); queue.push(toastDom); singleToast=true; return toastDom; } }; // 注冊(cè)為全局組件的函數(shù) function toast(options= {}) { const toastDom = createInstance(); toastDom.message =typeof options === 'string' ? options : options.message; toastDom.type = options.type || 'success'; toastDom.duration = options.duration || 3000; toastDom.position = options.position || 'middle'; if(!toastDom.message){ toastDom.showToast =singleToast= false; }else{ toastDom.showToast=true; setTimeout(() => {toastDom.showToast =singleToast= false} ,toastDom.duration); } } // 將組件注冊(cè)到 vue 的 原型鏈里去, // 這樣就可以在所有 vue 的實(shí)例里面使用 this.$toast() // Vue.prototype.$toast = showToast Vue.prototype.$toast = toast; export default toast
設(shè)置singleToast和queue的目的在于:確保同一時(shí)期界面上只有一個(gè)toast,不能同時(shí)出現(xiàn)多個(gè)toast。
由于toast會(huì)初始化,因此為了避免在任何操作之前界面上就出現(xiàn)一個(gè)toast,用if語句判斷:
如果沒有傳入的message,則不顯示toast(這樣可以使得初始化的toast不顯示)
否則顯示,并且過一定時(shí)間消失,只有singleToast為false,說明此刻界面上沒有toast,才能再新建一個(gè)toast實(shí)例(因?yàn)榇藭r(shí)if判斷內(nèi)queue.length 不為0【初始化的toast組件本身占了一個(gè)位置】,而singleToast為false,因此可以創(chuàng)建)
第三步:使用
在main.js 添加如下代碼:
import toast from './components/toast/index' Vue.use(toast)
創(chuàng)建需要調(diào)用的Vue文件:
<template> <div> <input type="button" value="顯示彈窗" @click="showToast"> </div> </template> <script> export default { methods: { showToast () { this.$toast({message:'加載中',type:'loading',position:'bottom', duration:'2000'}); // this.$toast('成功提示'); } } } </script>
可以看到一共兩種方式,可以以對(duì)象方式傳入?yún)?shù),也可以只傳入字符串,其他采用默認(rèn)設(shè)置。
總結(jié)
以上所述是小編給大家介紹的SVue自定義toast組件的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com