最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue自定義全局Toast和Loading的實(shí)例詳解

 更新時(shí)間:2019年04月18日 09:32:05   作者:darkerXi  
這篇文章主要介紹了Vue自定義全局Toast和Loading,需要的朋友可以參考下

如果我們的Vue項(xiàng)目中沒有用到任何UI框架的話,為了更好的用戶體驗(yàn),肯定會用到loading和toast。那么我們就自定義這兩個(gè)組件吧。

1、Toast組件

首先,在common下新建global文件夾,存放我們的toast.vue和toast.js兩個(gè)文件(當(dāng)然文件的具體位置你可以自行安排)。

(1). toast.vue

<template lang="html">
 <div v-if="isShowToast" class="toast-container" @touchmove.prevent>
  <!-- 這里content為雙花括號 -->
  <span class="loading-txt">{content}</span>
 </div>
</template>

<script>
export default {
 data () {
  return {
   isShowToast: true,
   content: ''
  }
 }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.toast-container {
 position: fixed;
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
 background: rgba(255, 255, 255, 0.1);
}
.toast-msg {
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
 width: 60%;
 padding: 35px;
 border-radius: 10px;
 font-size: 28px;
 line-height: 36px;
 background: #eee;
 color: #666;
}
</style>

(2). toast.js

import Vue from 'Vue'
import ToastComponent from './Toast.vue'

const Toast = {}
let showToast = false // 存儲loading顯示狀態(tài)
let toastNode = null // 存儲loading節(jié)點(diǎn)元素
const ToastConstructor = Vue.extend(ToastComponent)

Toast.install = function (Vue, options) {
 // 參數(shù)
 var opt = {
  duration: '1200'
 }
 for (var property in options) {
  opt[property] = options[property]
 }
 Vue.prototype.$toast = function (tips, type) {
  if (type === 'hide') {
   toastNode.isShowToast = showToast = false
  } else {
   if (showToast) {
    // 如果toast還在,則不再執(zhí)行
    return
   }
   toastNode = new ToastConstructor({
    data: {
     isShowToast: showToast,
     content: tips
    }
   })
   toastNode.$mount() // 掛在實(shí)例,為了獲取下面的toastNode.$el
   document.body.appendChild(toastNode.$el)
   toastNode.isShowToast = showToast = true
   setTimeout(function () {
    toastNode.isShowToast = showToast = false
   }, opt.duration)
  }
 };

 ['show', 'hide'].forEach(function (type) {
  Vue.prototype.$toast[type] = function (tips) {
   return Vue.prototype.$toast(tips, type)
  }
 })
}

export default Toast

然后,我們需要把寫好的組件在 /src/main.js 中引用一下。

import Toast from './components/common/global/toast'
Vue.use(Toast)

最后,怎么使用呢?只需在要用的地方this.$toast.show('hello world')

2、Loading組件

loading組件只需要照著toast組件搬過來,稍微改下就可以了。

首先,在common下新建global文件夾,存放我們的loading.vue和loading.js兩個(gè)文件。

(1). loading.vue

<template lang="html">
 <div v-if="isShowLoading" class="loading-container">
  <div class="loading-box">
   <img class="loading-img" :src="require('../../../assets/images/loading.png')">
   <!-- 這里content為雙花括號 -->
   <span class="loading-txt">{content}</span>
  </div>
 </div>
</template>

<script>
export default {
 data () {
  return {
   isShowLoading: false,
   content: ''
  }
 }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.loading-container {
 display: flex;
 justify-content: center;
 align-items: center;
 position: fixed;
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
 background: rgba(0, 0, 0, 0);
 z-index: 1000;
}
.loading-box {
 display: flex;
 flex-direction: column;
 justify-content: center;
 align-items: center;
 width: 150px;
 height: 150px;
 border-radius: 10px;
 background: #e5e5e5;
}
.loading-img {
 width: 70px;
 height: 70px;
 animation: rotating 2s linear infinite;
}
@keyframes rotating {
 0% {
  transform: rotate(0deg);
 }
 100% {
  transform: rotate(1turn);
 }
}
.loading-txt {
 display: flex;
 justify-content: center;
 align-items: center;
 font-size: 24px;
 color: #666;
}
</style>

(2). loading.js

import Vue from 'Vue'
import LoadingComponent from './Loading.vue'

const Loading = {}
let showLoading = false // 存儲loading顯示狀態(tài)
let loadingNode = null // 存儲loading節(jié)點(diǎn)元素
const LoadingConstructor = Vue.extend(LoadingComponent)

Loading.install = function (Vue) {
 Vue.prototype.$loading = function (tips, type) {
  if (type === 'hide') {
   loadingNode.isShowLoading = showLoading = false
  } else {
   if (showLoading) {
    // 如果loading還在,則不再執(zhí)行
    return
   }
   loadingNode = new LoadingConstructor({
    data: {
     isShowLoading: showLoading,
     content: tips
    }
   })
   loadingNode.$mount() // 掛在實(shí)例,為了獲取下面的loadingNode.$el
   document.body.appendChild(loadingNode.$el)
   loadingNode.isShowLoading = showLoading = true
  }
 };

 ['show', 'hide'].forEach(function (type) {
  Vue.prototype.$loading[type] = function (tips) {
   return Vue.prototype.$loading(tips, type)
  }
 })
}

export default Loading

然后,在 /src/main.js 中引用一下loading組件。

import Loading from './components/common/global/loading'
Vue.use(Loading)

最后,只需在要用的地方this.$loading.show('hello world')、 this.$loading.hide()

總結(jié)

以上所述是小編給大家介紹的Vue自定義全局Toast和Loading的實(shí)例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • 基于vue+elementPlus的動態(tài)導(dǎo)航標(biāo)簽欄tabs具體過程

    基于vue+elementPlus的動態(tài)導(dǎo)航標(biāo)簽欄tabs具體過程

    這篇文章主要給大家介紹了關(guān)于基于vue+elementPlus的動態(tài)導(dǎo)航標(biāo)簽欄tabs的相關(guān)資料,本文主要詳述了在系統(tǒng)上添加導(dǎo)航標(biāo)簽欄功能時(shí),首次嘗試的過程,并且希望能為同行提供一個(gè)小demo,需要的朋友可以參考下
    2024-10-10
  • vue element-ui table表格滾動加載方法

    vue element-ui table表格滾動加載方法

    下面小編就為大家分享一篇vue element-ui table表格滾動加載方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • 詳解VS Code使用之Vue工程配置format代碼格式化

    詳解VS Code使用之Vue工程配置format代碼格式化

    這篇文章主要介紹了詳解VS Code使用之Vue工程配置format代碼格式化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • vue3表單輸入綁定方式

    vue3表單輸入綁定方式

    這篇文章主要介紹了vue3表單輸入綁定方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Vue2.x中的父組件傳遞數(shù)據(jù)至子組件的方法

    Vue2.x中的父組件傳遞數(shù)據(jù)至子組件的方法

    這篇文章主要介紹了Vue2.x中的父組件數(shù)據(jù)傳遞至子組件的方法,需要的朋友可以參考下
    2017-05-05
  • Vue使用epubjs電子書的教程詳解

    Vue使用epubjs電子書的教程詳解

    EPUB.js是一個(gè)基于JavaScript的庫,用于從電子書中提取內(nèi)容,這篇文章主要為大家詳細(xì)介紹了vue如何使用epubjs實(shí)現(xiàn)電子書的功能,感興趣的小伙伴可以學(xué)習(xí)一下
    2023-11-11
  • vue 2.5.1 源碼學(xué)習(xí) 之Vue.extend 和 data的合并策略

    vue 2.5.1 源碼學(xué)習(xí) 之Vue.extend 和 data的合并策略

    這篇文章主要介紹了Vue.extend 和 data的合并策略 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • Vue判斷字符串(或數(shù)組)中是否包含某個(gè)元素的多種方法

    Vue判斷字符串(或數(shù)組)中是否包含某個(gè)元素的多種方法

    在我們前端日常開發(fā)中經(jīng)常會遇到判斷一個(gè)字符串中是否包含某個(gè)元素的需求,下面這篇文章主要給大家介紹了關(guān)于Vue判斷字符串(或數(shù)組)中是否包含某個(gè)元素的多種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • Vue 使用計(jì)時(shí)器實(shí)現(xiàn)跑馬燈效果的實(shí)例代碼

    Vue 使用計(jì)時(shí)器實(shí)現(xiàn)跑馬燈效果的實(shí)例代碼

    這篇文章主要介紹了Vue 使用計(jì)時(shí)器實(shí)現(xiàn)跑馬燈效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-07-07
  • el-form中的rules未生效的解決方法

    el-form中的rules未生效的解決方法

    本文主要介紹了el-form中的rules未生效的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評論

乌海市| 连江县| 景德镇市| 宝清县| 贡山| 抚顺市| 丰都县| 张家界市| 蓬安县| 习水县| 松阳县| 永嘉县| 东安县| 明水县| 三门峡市| 紫金县| 舒兰市| 哈密市| 台安县| 沈阳市| 辉南县| 全南县| 永仁县| 通州区| 星座| 舞阳县| 龙井市| 嘉黎县| 绥棱县| 曲周县| 江西省| 雅安市| 上林县| 凤城市| 富源县| 托克逊县| 顺义区| 浏阳市| 宿州市| 航空| 蓝山县|