Vue的Ui组件库中的loading不会用怎么办?轮子大法好

先上原文链接:
添加链接描述
1、一个文件夹,一个loading.js 一个loading.vue,一个请求拦截的文件
(1)loading.js

 import Loading from './loading.vue';
  import Vue from 'vue';
  export default()=>{
      let LoadingComponent=Vue.extend(Loading);
      let child=new LoadingComponent({//动态创建挂载点
          el:document.createElement("div"),
          data:{
              flag:false,
          },
        methods:{
             handlemount(){
                this.flag=true;
           },
             handleDestory(){
                this.flag=false;
            }
         }
     })
    document.body.appendChild(child.$mount().$el)
     return child;
 }

(2)vue文件夹

 <template>
       <div class="loader" v-if="flag">
           <!-- //利用v-if操控插件的显示与消失。 -->
       <div class="loader-inner">
           <div class="loader-line-wrap">
               <div class="loader-line"></div>
           </div>
           <div class="loader-line-wrap">
               <div class="loader-line"></div>
           </div>
          <div class="loader-line-wrap">
              <div class="loader-line"></div>
          </div>
          <div class="loader-line-wrap">
              <div class="loader-line"></div>
          </div>
          <div class="loader-line-wrap">
              <div class="loader-line"></div>
          </div>
      </div>
  </div>
  </template>
  
  <script>
  export default {
      name:"Movie" 
  }
 </script>
  
<style scoped lang="less">
      .loader {
      background: #000;
      background: radial-gradient(#222, #000);
     bottom: 0;
     left: 0;
      overflow: hidden;
      position: fixed;
     right: 0;
     top: 0;
      z-index: 99999;
  }
  
 .loader-inner {
     bottom: 0;
     height: 60px;
      left: 0;
     margin: auto;
      position: absolute;
      right: 0;
      top: 0;
      width: 100px;
  }
  
  .loader-line-wrap {
      animation: 
          spin 2000ms cubic-bezier(.175, .885, .32, 1.275) infinite
      ;
      box-sizing: border-box;
     height: 50px;
     left: 0;
      overflow: hidden;
      position: absolute;
      top: 0;
     transform-origin: 50% 100%;
      width: 100px;
 }
  .loader-line {
     border: 4px solid transparent;
      border-radius: 100%;
     box-sizing: border-box;
     height: 100px;
      left: 0;
      margin: 0 auto;
      position: absolute;
     right: 0;
      top: 0;
     width: 100px;
  }
 .loader-line-wrap:nth-child(1) { animation-delay: -50ms; }
  .loader-line-wrap:nth-child(2) { animation-delay: -100ms; }
 .loader-line-wrap:nth-child(3) { animation-delay: -150ms; }
  .loader-line-wrap:nth-child(4) { animation-delay: -200ms; }
  .loader-line-wrap:nth-child(5) { animation-delay: -250ms; }
  
  .loader-line-wrap:nth-child(1) .loader-line {
      border-color: hsl(0, 80%, 60%);
     height: 90px;
      width: 90px;
      top: 7px;
  }
  .loader-line-wrap:nth-child(2) .loader-line {
      border-color: hsl(60, 80%, 60%);
      height: 76px;
      width: 76px;
      top: 14px;
  }
  .loader-line-wrap:nth-child(3) .loader-line {
     border-color: hsl(120, 80%, 60%);
      height: 62px;
      width: 62px;
     top: 21px;
 }
 .loader-line-wrap:nth-child(4) .loader-line {
     border-color: hsl(180, 80%, 60%);
    height: 48px;
    width: 48px;
     top: 28px;
 }
 .loader-line-wrap:nth-child(5) .loader-line {
     border-color: hsl(240, 80%, 60%);
     height: 34px;
    width: 34px;
     top: 35px;
 }
 
 @keyframes spin {
    0%, 15% {
        transform: rotate(0);
     }
     100% {
         transform: rotate(360deg);
     }
 }
 </style>

3、请求拦截文件

import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import { VueAxios } from './axios'
import {Modal, notification,message} from 'ant-design-vue'
import { ACCESS_TOKEN } from "@/store/mutation-types"
// loading
import loading from 'url..........................'
let vm=loading()


let apiBaseUrl = window._CONFIG['domianURL'] || "/jeecg-boot";
console.log("apiBaseUrl= ",apiBaseUrl)
// 创建 axios 实例
const service = axios.create({
  
  baseURL: apiBaseUrl, // api base_url
  timeout: 9000 // 请求超时时间
})

const err = (error) => {
  if(error.message.includes('timeout')){
    message.error('请求超时!')
}
  if (error.response) {
    let data = error.response.data
    const token = Vue.ls.get(ACCESS_TOKEN)
    console.log("------异常响应------",token)
    console.log("------异常响应------",error.response.status)
    switch (error.response.status) {
      case 403:
        notification.error({ message: '系统提示', description: '拒绝访问',duration: 4})
        break
      case 500:
        //notification.error({ message: '系统提示', description:'Token失效,请重新登录!',duration: 4})
        if(token && data.message=="Token失效,请重新登录"){
          // update-begin- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转----
          // store.dispatch('Logout').then(() => {
          //     window.location.reload()
          // })
          Modal.error({
            title: '登录已过期',
            content: '很抱歉,登录已过期,请重新登录',
            okText: '重新登录',
            mask: false,
            onOk: () => {
              store.dispatch('Logout').then(() => {
                Vue.ls.remove(ACCESS_TOKEN)
                window.location.reload()
              })
            }
          })
          // update-end- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转----
        }
        break
      case 404:
          notification.error({ message: '系统提示', description:'很抱歉,资源未找到!',duration: 4})
        break
      case 504:
        notification.error({ message: '系统提示', description: '网络超时'})
        break
      case 401:
        notification.error({ message: '系统提示', description:'未授权,请重新登录',duration: 4})
        if (token) {
          store.dispatch('Logout').then(() => {
            setTimeout(() => {
              window.location.reload()
            }, 1500)
          })
        }
        break
      default:
        notification.error({
          message: '系统提示',
          description: data.message,
          duration: 4
        })
        break
    }
  }
  return Promise.reject(error)
};


// request interceptor
service.interceptors.request.use(config => {

  const token = Vue.ls.get(ACCESS_TOKEN)
  if (token) {
    config.headers[ 'X-Access-Token' ] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  if(config.method=='get'){
    if(config.url.indexOf("sys/dict/getDictItems")<0){
      config.params = {
        _t: Date.parse(new Date())/1000,
        ...config.params
      }
    }
  }
// loadin
  vm.handlemount();

  return config
},(error) => {

  return Promise.reject(error)
})

// response interceptor
service.interceptors.response.use(
  (response) => {
    // loading
    vm.handleDestory();
    return response.data
  },err)

const installer = {
  vm: {},
  install (Vue, router = {}) {
    Vue.use(VueAxios, router, service)
  }
}

export {
  installer as VueAxios,
  service as axios
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值