一、main.js设置拦截器

  1. npm i axios -S
  1. import axios from 'axios'
  2. Vue.prototype.axios = axios;
  3. //定义一个请求拦截器
  4. axios.interceptors.request.use(function(config){
  5. store.state.isShow=true; //在请求发出之前进行一些操作
  6. return config
  7. })
  8. //定义一个响应拦截器
  9. axios.interceptors.response.use(function(config){
  10. store.state.isShow=false;//在这里对返回的数据进行处理
  11. return config
  12. })

二、Vuex state状态设置控制loading的显隐

  1. }
  2. export default new Vuex.Store({
  3. state: {
  4. isShow:false
  5. }
  6. })

三、设置Loading

  1. <Loading v-if="this.$store.state.isShow"></Loading>