先寫好一個加載動畫組件,如:javascript
<template> <view class="request-loading-view" v-show="loadingShow"> <view class="loading-view"><view class="loading"></view></view> </view> </template> <script> export default { data() { return {}; }, computed: { //計算屬性判斷vuex中的顯示狀態 loadingShow() { return this.$store.state.requestLoading; } } }; </script> <style scoped> .request-loading-view { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 198903060; background-color: rgba(0, 0, 0, 0.001); display: flex; justify-content: center; align-items: center; } .loading-view { width: 160upx; height: 160upx; background-color: rgba(0, 0, 0, 0.6); border-radius: 20upx; display: flex; justify-content: center; align-items: center; } /* 動畫樣式 */ .loading { border: 10upx solid rgba(0, 0, 0, 0.01); border-radius: 50%; border-top: 10upx solid #fff; border-right: 10upx solid #fff; border-bottom: 10upx solid #fff; width: 60upx; height: 60upx; -webkit-animation: spin 1.4s linear infinite; animation: spin 1.4s linear infinite; } @-webkit-keyframes spin { 0% { -webkit-transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); } } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } </style>
main.js 中添加相應引用,使用Vuex來記錄顯示狀態,因此Vuex也須要引用vue
//Vuex import store from './store' Vue.prototype.$store = store //請求加載組件 import requestLoading from './components/compt_requestLoading.vue'; //組件掛載到全局,方便每一個頁面使用 Vue.component('request-loading', requestLoading); //掛載全局顯示/隱藏請求加載動畫 function showLoading(){ store.commit('request_show_loading'); } function hideLoading(){ store.commit('request_hide_loading'); } Vue.prototype.$showLoading = showLoading; //全局顯示動畫能夠 this.$showLoading(); Vue.prototype.$hideLoading = hideLoading; //全局隱藏動畫能夠 this.$hideLoading();
Vuex的store/index.js中這樣寫java
//Vuex import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { requestLoading: false //加載等待是否顯示 }, mutations: { //顯示請求加載動畫 request_show_loading(state) { state.requestLoading = true; }, //隱藏請求加載動畫 request_hide_loading(state) { state.requestLoading = false; } } }) export default store
而後每一個頁面添加標籤web
<request-loading></request-loading>
便可vuex
調用顯示/隱藏能夠直接ide
this.$showLoading() this.$hideLoading()