zoukankan      html  css  js  c++  java
  • vue axios拦截器加全局loading

    import axios from 'axios'
    import util from './util'
    import {showFullScreenLoading, tryHideFullScreenLoading} from './axiosHelperLoading'
    
    export default function () {
        axios.interceptors.request.use(
            req => {
                req.headers.token = localStorage.getItem('token') || '';
                showFullScreenLoading();
                return req
            },
            err => {
                return Promise.reject(err)
            }
        );
        axios.interceptors.response.use(
            res => {
                if (res.data.code === -2004) {
                    // 登录失效,清楚token,刷新页面
                    util.localStorage.remove('token');
                    window.location.href = window.location.origin + "/#/login";
                    return;
                }
                if (res.data.code === -2006) {
                    window.location.href = window.location.origin + "/#/unauthorizedPage";
                    return;
                }
                if (res.status !== 200 || res.data.code === '-1') {
                    return Promise.reject(res)
                }
                tryHideFullScreenLoading();
                return res;
            },
            err => {
                return Promise.reject(err)
            }
        )
    }
    

      loading下面

    import {Loading} from 'element-ui';
    import _ from 'lodash';
    let needLoadingRequestCount = 0;
    let loading;
    
    function startLoading () {
        loading = Loading.service({
            lock: true,
            text: '加载中……',
            background: 'rgba(0, 0, 0, 0.7)'
        })
    }
    
    function endLoading() {
        loading.close()
    }
    
    const tryCloseLoading = () => {
        if (needLoadingRequestCount === 0) {
            endLoading();
        }
    };
    
    export function showFullScreenLoading () {
        if (needLoadingRequestCount === 0) {
            startLoading();
        }
        needLoadingRequestCount++;
    }
    
    export function tryHideFullScreenLoading () {
        if (needLoadingRequestCount <= 0) return;
        needLoadingRequestCount--;
        if (needLoadingRequestCount === 0) {
            _.debounce(tryCloseLoading, 300)()
        }
    }
  • 相关阅读:
    c# winform 读取图片列表
    C# 枚举显示中文
    onenote网页版如何打开链接弄到客户端
    the error code is 2203
    unknown software exception
    无法读取配置节“protocolMapping”,因为它缺少节声明
    oracle 索引失效原因
    向 mysql 插入汉字时报错 Incorrect string value: 'xE6x9BxB9xE5x86xAC...' for col....
    (二)Linux——Linux常用指令
    (一)Linux——Linux基本概念
  • 原文地址:https://www.cnblogs.com/jintaostudy/p/9550276.html
Copyright © 2011-2022 走看看