vant的Loading加載動(dòng)畫(huà)組件的使用(通過(guò)接口拿數(shù)據(jù)時(shí)顯示加載狀態(tài))
在store.js中使用vuex全局控制loading顯示與隱藏
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
LOADING: false
},
mutations: {
showLoading(state) {
state.LOADING = true
},
hideLoading(state) {
state.LOADING = false
}
}新建loading公共組件頁(yè)面
<template>
<div class="loading">
<van-loading type="spinner" color="#1989fa" />
</div>
</template>
<script>
import Vue from 'vue';
import {Loading} from 'vant';
Vue.use(Loading);
export default {
name: 'LOADING',
data() {
return {}
},
}
</script>
<style lang="scss" scoped>
.loading {
position: fixed;
z-index: 9999;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.5);
display: flex;
justify-content: center;
align-items: center;
}
</style>在App.vue中,將loading組件掛載到工程根節(jié)點(diǎn)
掛載到App.vue中之后所有的接口請(qǐng)求都會(huì)加載loading組件
可以在需要的頁(yè)面單獨(dú)引用
<template>
<div id="app">
<Loading v-show="LOADING"></Loading>
......//其他代碼
</div>
</template>
<script>
import {mapState} from 'vuex'
import Loading from '@c/Loading.vue'
export default {
// ...解構(gòu),將store中的state進(jìn)行映射。
// 在template中可以直接使用,不需要通過(guò)this.$store.state一個(gè)個(gè)獲取store中的state數(shù)據(jù)。
computed: {
...mapState(['LOADING'])
},
name: 'App',
components: {Loading}
}
</script>在請(qǐng)求攔截器和響應(yīng)攔截器中配置
在封裝好的axios中,利用axios的攔截器實(shí)現(xiàn)請(qǐng)求時(shí)提交store顯示loading;
請(qǐng)求失敗或者完成提交store隱藏loading。
import Vue from "vue";
import axios from 'axios';
import store from '../../store';
// 請(qǐng)求攔截器
axios.interceptors.request.use(function (config) {
store.commit('showLoading')
return config;
}, function (error) {
store.commit('hideLoading')
return Promise.reject(error);
});
//響應(yīng)攔截器
axios.interceptors.response.use((response) => {
store.commit('hideLoading')
return response.data;
}, (error) => {
store.commit('hideLoading')
return Promise.reject(error);
});
//綁定到vue原型中
Vue.prototype.$http = axios;如果在單個(gè)請(qǐng)求中使用
// 在請(qǐng)求時(shí)
this.$store.commit('showLoading')
//請(qǐng)求完成后
this.$store.commit('hideLoading')到此這篇關(guān)于vant的Loading加載動(dòng)畫(huà)組件的使用,通過(guò)接口拿數(shù)據(jù)時(shí)顯示加載狀態(tài)的文章就介紹到這了,更多相關(guān)vant Loading加載動(dòng)畫(huà)組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用vue2.0創(chuàng)建的項(xiàng)目的步驟方法
這篇文章主要介紹了使用vue2.0創(chuàng)建的項(xiàng)目的步驟方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue使用extend動(dòng)態(tài)創(chuàng)建組件的實(shí)現(xiàn)
本文主要介紹了Vue使用extend動(dòng)態(tài)創(chuàng)建組件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Vue 頁(yè)面監(jiān)聽(tīng)用戶(hù)預(yù)覽時(shí)間功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue 頁(yè)面如何監(jiān)聽(tīng)用戶(hù)預(yù)覽時(shí)間,首先需要借助vue頁(yè)面的生命周期函數(shù)mounted和destroyed,分別加入開(kāi)始計(jì)時(shí)和清除計(jì)時(shí)的邏輯,通過(guò)相關(guān)操作實(shí)現(xiàn)此功能,需要的朋友可以參考下2021-07-07
vue2.0實(shí)現(xiàn)列表數(shù)據(jù)增加和刪除
這篇文章主要為大家詳細(xì)介紹了vue2.0實(shí)現(xiàn)列表數(shù)據(jù)增加和刪除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
vue+vuex+axios從后臺(tái)獲取數(shù)據(jù)存入vuex,組件之間共享數(shù)據(jù)操作
這篇文章主要介紹了vue+vuex+axios從后臺(tái)獲取數(shù)據(jù)存入vuex,組件之間共享數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
vue + node如何通過(guò)一個(gè)Txt文件批量生成MP3并壓縮成Zip
這篇文章主要介紹了vue + node如何通過(guò)一個(gè)Txt文件批量生成MP3并壓縮成Zip的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
一文詳解如何創(chuàng)建Vue3項(xiàng)目及組合式API
Vue3提供了一種組合式API,可以用來(lái)構(gòu)建可復(fù)用的組件和應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于如何創(chuàng)建Vue3項(xiàng)目及組合式API的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05

