axios 攔截器顯示和關閉Loading

使用Loading分爲2種狀況,第一種是使用一些組件庫自帶的loading,另外一種是使用咱們本身寫的loading,現分開介紹使用方法javascript

1、使用element ui 帶的Loading

一、在main.js 中引入axios 和elementuicss

// 引入element-ui 組件
import ElementUI from 'element-ui'
// 引入element-ui 樣式文件
import 'element-ui/lib/theme-chalk/index.css'
import axios from "axios"
Vue.use(ElementUI)
Vue.prototype.$axios = axios

二、在main.js 中設置Loading 顯示和關閉函數html

let loading;
function startLoading() {
	//若是根實例設爲變量VUE var VUE = new Vue({}) 也可寫成下面的 
    // loading = VUE.$loading({
    //   lock: true,
    //   text: "拼命加載中...",
    //   background: 'rgba(0,0,0,0.2)'
    // })
    loading = ElementUI.Loading.service({
        lock: true,
        text: "加載中...",
        background: 'rgba(0,0,0,0.2)'
    })
}

function endLoading() {
    loading.close()
}

三、一樣在main.js 中設置請求和響應攔截器vue

// 請求數據攔截器
axios.interceptors.request.use(config => {
    console.log("攔截器")
    startLoading()
    return config
})
// 響應數據攔截器
axios.interceptors.response.use(response => {
    endLoading()
    return response
})

2、使用本身在頁面中寫的Loading

一、新建一個loading.vue 組件java

<template>
  <div class="loader">
    <div class="loading" id="loading">
      <div class="animateWrap">
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      </div>
      <div class="text">正在加載</div>
    </div>
  </div>
</template>

<script>
// import theme from '@/assets/js/theme/shine.js'
export default {
  data() {
    return {};
  },

  methods: {},
  mounted(){
    var spanli = document.getElementById("loading").getElementsByTagName("span");
    for(var i=0;i<spanli.length;i++){
      spanli[i].style.webkitAnimationDelay = 0.13*i+"s"
    }
  }
};
</script>
<style>
.loader {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 20;
}
.loading {
  position: absolute;
  width: 70px;
  height: 70px;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
 
}
.loading span {
  display: inline-block;
  width: 8px;
  height: 30px;
  border-radius: 3px;
  margin: 3px;
  background: #5af3f3;
  -webkit-animation:line-square 1s infinite;
}
.text{color:#5af3f3;}
@-webkit-keyframes line-square{
  0%{
    transform:scaleY(1)
  }
  50%{
    transform:scaleY(0.3)
  }
  100%{
    transform:scaleY(1)
  }
}
</style>

二、在App.vue 引入註冊並使用loading.vue組件ios

<template>
    <div id="app">
        <ul class="nav" ref="nav">
            <router-link tag="li" :to='{name:"home"}'>首頁</router-link>
            <router-link tag="li" :to="{name:'chart'}">圖表</router-link>
            <router-link tag="li" :to="{name:'exportTable'}">表格</router-link>
            <router-link tag="li" :to="{name:'formTest'}">表單測試</router-link>
            <router-link tag="li" :to="{name:'layoutContainer'}">佈局容器</router-link>
        </ul>
        {{message}}
        <router-view />
        <Loading v-if="this.$store.state.loadingShow" />
    </div>
</template>

<script>
import Loading from "@/components/loading.vue"
export default {
  name: 'App',
  data(){
    return{
     
    }    
  },
  components:{Loading},
}
</script>

三、在store 中初始化loadingShowweb

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    loadingShow:false
  },
  mutations: {
  },
  actions: {
  },
  modules: {
  }
})

四、在main.js中設置請求和響應攔截器vuex

// 請求數據攔截器
axios.interceptors.request.use(config => {
    console.log("攔截器")
    //startLoading()
    store.state.loadingShow = true
    return config
})
// 響應數據攔截器
axios.interceptors.response.use(response => {
    //endLoading()
    store.state.loadingShow = false
    return response
})
相關文章
相關標籤/搜索