内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何在Vue中自定義一個(gè)toast組件

本篇文章為大家展示了如何在Vue中自定義一個(gè)toast組件,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

目前成都創(chuàng)新互聯(lián)公司已為上1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、如東網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

第一步:寫(xiě)toast.vue,將樣式之類的先定下來(lái)

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

如何在Vue中自定義一個(gè)toast組件如何在Vue中自定義一個(gè)toast組件如何在Vue中自定義一個(gè)toast組件

一共三種樣式,成功(success),失敗(wrong),加載中(loading);

一共三種位置,上(top),中(middle),下(bottom);

所有涉及的圖案出自阿里的iconfont 手機(jī)淘寶圖標(biāo)庫(kù)。

加載中動(dòng)畫(huà)是自己寫(xiě)的蹩腳的加載組件(emmm,就不放出來(lái)污染大家眼睛了,需要的可以評(píng)論區(qū)知會(huì)一聲_(:з」∠)_)

第二步:寫(xiě)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語(yǔ)句判斷:

如果沒(méi)有傳入的message,則不顯示toast(這樣可以使得初始化的toast不顯示)

否則顯示,并且過(guò)一定時(shí)間消失,只有singleToast為false,說(shuō)明此刻界面上沒(méi)有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>

如何在Vue中自定義一個(gè)toast組件

上述內(nèi)容就是如何在Vue中自定義一個(gè)toast組件,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站標(biāo)題:如何在Vue中自定義一個(gè)toast組件
當(dāng)前鏈接:http://www.rwnh.cn/article0/jdieoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT用戶體驗(yàn)、軟件開(kāi)發(fā)網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計(jì)定制開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(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)

成都app開(kāi)發(fā)公司
梁山县| 南投市| 宿州市| 云浮市| 张家口市| 安塞县| 来凤县| 株洲县| 青铜峡市| 阳原县| 嵩明县| 河曲县| 麻江县| 黄骅市| 右玉县| 濮阳县| 望都县| 长治市| 沂源县| 南宁市| 永嘉县| 宁安市| 澄迈县| 钟祥市| 隆昌县| 崇左市| 浏阳市| 隆安县| 华容县| 安化县| 乌拉特前旗| 宣威市| 德清县| 含山县| 闵行区| 安陆市| 和平县| 江陵县| 兴宁市| 永定县| 庐江县|