vue axios全局login

// main.js

// 表示使用 this.$http.get() 会自动在路径前面加上/api const httpInstance = axios.create({ baseURL: '/api' }) //请求拦截 httpInstance.interceptors.request.use(config => { // if (!config.noShowLoading) { // 不显示loading // requestStore.updateUnderwayCount(1) // } if (config.params) { const encodeParam = {} Object.keys(config.params).forEach(key => { const p = config.params[key] if(key !== 'telList') { if (typeof p === 'object') { encodeParam[key] = p delete config.params[key] } } }) config.params.encode = encodeParam } return config }) //请求响应 httpInstance2.interceptors.response.use(res => { // if (!res.config.noShowLoading) { // 不显示loading // requestStore.updateUnderwayCount(-1) // } // debugger if (res.code === '302') { // 未登录 store.dispatch('popupTips', { text: '请先登陆', s: 1000, fn() { location.replace('/login?sourceurl=' + encodeURIComponent(location.href) ) } }) } res.getData = () => Promise.reject(res.data) return res })
// 全局注册 别的组件里可以直接使用 this.$http.get()方法了
Vue.prototype.$http = httpInstance

window.$http = httpInstance
 

components/Login.vue

<template>
  <div v-show="requestCount" class="loadingAm">
    <div class="spinner">
      <div class="spinner-container container1">
        <div class="circle1"></div>
        <div class="circle2"></div>
        <div class="circle3"></div>
        <div class="circle4"></div>
      </div>
      <div class="spinner-container container2">
        <div class="circle1"></div>
        <div class="circle2"></div>
        <div class="circle3"></div>
        <div class="circle4"></div>
      </div>
      <div class="spinner-container container3">
        <div class="circle1"></div>
        <div class="circle2"></div>
        <div class="circle3"></div>
        <div class="circle4"></div>
      </div>
    </div>
  </div>
</template>
<script>
  export default{
    data(){
      return {
        requestCount: 0
      }
    },
    mounted(){
      const that = this
      that.$http.interceptors.request.use(config => {
        that.requestCount++
        return config
      }, function (error) {
        that.requestCount--
        return Promise.reject(error)
      })

      that.$http.interceptors.response.use(response => {
        that.requestCount--
        return response
      }, function (error) {
        that.requestCount--
        return Promise.reject(error)
      })
    }
  }
</script>

<style lang="scss">
  .loadingAm {
    position: fixed;
    left: 50%;
    top: 30%;
    margin-left: -60px;
    padding: 30px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 10px;
    z-index: 100;
  }

  .spinner {
     40px;
    height: 40px;
    position: relative;;
  }

  .container1 > div, .container2 > div, .container3 > div {
     12px;
    height: 12px;
    background-color: #FFF;

    border-radius: 100%;
    position: absolute;
    animation: bouncedelay 1.2s infinite ease-in-out;
    animation-fill-mode: both;
  }

  .spinner .spinner-container {
    position: absolute;
     100%;
    height: 100%;
  }

  .container2 {
    transform: rotateZ(45deg);
  }

  .container3 {
    transform: rotateZ(90deg);
  }

  .circle1 {
    top: 0;
    left: 0;
  }

  .circle2 {
    top: 0;
    right: 0;
  }

  .circle3 {
    right: 0;
    bottom: 0;
  }

  .circle4 {
    left: 0;
    bottom: 0;
  }

  .container2 .circle1 {
    animation-delay: -1.1s;
  }

  .container3 .circle1 {
    animation-delay: -1.0s;
  }

  .container1 .circle2 {
    animation-delay: -0.9s;
  }

  .container2 .circle2 {
    animation-delay: -0.8s;
  }

  .container3 .circle2 {
    animation-delay: -0.7s;
  }

  .container1 .circle3 {
    animation-delay: -0.6s;
  }

  .container2 .circle3 {
    animation-delay: -0.5s;
  }

  .container3 .circle3 {
    animation-delay: -0.4s;
  }

  .container1 .circle4 {
    animation-delay: -0.3s;
  }

  .container2 .circle4 {
    animation-delay: -0.2s;
  }

  .container3 .circle4 {
    animation-delay: -0.1s;
  }

  @keyframes bouncedelay {
    0%, 80%, 100% {
      transform: scale(0.0);
    }
    40% {
      transform: scale(1.0);
    }
  }

</style>

最后在App.vue中引入即可

或者使用element的也是同理,++ 或者 --

一辈子说长不长,说短不短,努力做好两件事:第一件事爱生活,爱身边的人,爱自己;第二件事是好好学习,好好工作,实现自己的人生价值观,而不仅仅是为了赚钱
原文地址:https://www.cnblogs.com/dcyd/p/14419889.html