官网http://axios-js.com/zh-cn/docs/index.html#%E6%8B%A6%E6%88%AA%E5%99%A8

7.vuex,axios拦截器实现Loading加载条 - 图2

7.1main.js设置axios拦截器

  1. yarn add axios
  2. import axios from 'axios'
  3. Vue.prototype.axios=axios;
  4. axios.interceptors.request.use(function (config) {
  5. store.state.isLoading=true;
  6. return config;
  7. })
  8. axios.interceptors.response.use(function (response) {
  9. store.state.isLoading=false;
  10. return response;
  11. });
  12. //使用vant-ui实现loading
  13. 1.安装依赖
  14. yarn add vant
  15. 2.引入依赖
  16. import Vant from 'vant'
  17. import 'vant/lib/index.css'
  18. import { Loading } from 'vant';
  19. Vue.use(Loading); //注意这个有顺序关系,放在引入的下面
  20. Vue.use(Vant)

7.2vuex

  1. //store/index.js
  2. export default new Vuex.Store({
  3. state:{
  4. isLoading:true
  5. },
  6. ...
  7. })

7.3在全局组件里面使用—App.vue

  1. <template>
  2. <div id="app">
  3. <van-loading v-if="this.$store.state.isLoading"
  4. size="24px" type="spinner" color="#1989fa">加载中...</van-loading>
  5. ...
  6. </div>
  7. </template>