vant的Loading加載動畫組件的使用(通過接口拿數(shù)據(jù)時顯示加載狀態(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公共組件頁面
<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中之后所有的接口請求都會加載loading組件
可以在需要的頁面單獨(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中可以直接使用,不需要通過this.$store.state一個個獲取store中的state數(shù)據(jù)。 computed: { ...mapState(['LOADING']) }, name: 'App', components: {Loading} } </script>
在請求攔截器和響應(yīng)攔截器中配置
在封裝好的axios中,利用axios的攔截器實(shí)現(xiàn)請求時提交store顯示loading;
請求失敗或者完成提交store隱藏loading。
import Vue from "vue"; import axios from 'axios'; import store from '../../store'; // 請求攔截器 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;
如果在單個請求中使用
// 在請求時 this.$store.commit('showLoading') //請求完成后 this.$store.commit('hideLoading')
到此這篇關(guān)于vant的Loading加載動畫組件的使用,通過接口拿數(shù)據(jù)時顯示加載狀態(tài)的文章就介紹到這了,更多相關(guān)vant Loading加載動畫組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue?打包優(yōu)化之externals抽離公共的第三方庫詳解
這篇文章主要為大家介紹了Vue?打包優(yōu)化之externals抽離公共的第三方庫詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-06-06基于Vue3+Element Plus 實(shí)現(xiàn)多表單校驗(yàn)demo
表單校驗(yàn)在日常的開發(fā)需求中是一種很常見的需求,通常在提交表單發(fā)起請求前校驗(yàn)用戶輸入是否符合規(guī)則,通常只需formRef.value.validate()即可校驗(yàn),本文給大家介紹基于Vue3+Element Plus 實(shí)現(xiàn)多表單校驗(yàn)demo,感興趣的朋友一起看看吧2024-06-06Vue2打包部署后可動態(tài)修改后端接口地址的解決方法
本篇文章將介紹使用Vue2開發(fā)前后端分離項(xiàng)目時,前端打包部署后可動態(tài)修改后端接口地址的解決方法,文中通過圖文結(jié)合的方式介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07Vue-cli 如何將px轉(zhuǎn)化為rem適配移動端
這篇文章主要介紹了Vue-cli 如何將px轉(zhuǎn)化為rem適配移動端,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07