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)()
}
}

vue axios拦截器加全局loading的更多相关文章

  1. vue axios拦截器 + 自编写插件 实现全局 loading 效果;

    项目需求:用自定义的 .gif 图标实现全局 loading 效果:为避免在每个页面手动添加,且简单高效的实现,经查阅资料,最终采用了 vue axios拦截器 + 自编写 loading 插件:下面 ...

  2. vue --- axios拦截器+form格式请求体

    在vue2.x中使用CLI生成的模板有很大改变,需要自己手动在main.ts同级目录下新建interceptors.ts interceptors.ts import axios from 'axio ...

  3. vue axios 拦截器

    前言 项目中需要验证登录用户身份是否过期,是否有权限进行操作,所以需要根据后台返回不同的状态码进行判断. 第一次使用拦截器,文章中如有不对的地方还请各位大佬帮忙指正谢谢. 正文 axios的拦截器分为 ...

  4. vue axios拦截器的封装

    // request.js import axios from 'axios' import qs from 'qs' // 创建axios实例 const service = axios.creat ...

  5. vue axios拦截器介绍

    关于axios的拦截器是一个作用非常大,非常好用的东西.分为请求拦截器和响应拦截器两种.我一般把拦截器写在main.js里. 1. 请求拦截器 请求拦截器的作用是在请求发送前进行一些操作,例如在每个请 ...

  6. Vue基于vuex、axios拦截器实现loading效果及axios的安装配置

    准备 利用vue-cli脚手架创建项目 进入项目安装vuex.axios(npm install vuex,npm install axios) axios配置 项目中安装axios模块(npm in ...

  7. vue导航守卫和axios拦截器的区别

    在Vue项目中,有两种用户登录状态判断并处理的情况,分别为:导航守卫和axios拦截器. 一.什么是导航守卫? vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.(在路由跳转时 ...

  8. 12. 前后端联调 + ( proxy代理 ) + ( axios拦截器 ) + ( css Modules模块化方案 ) + ( css-loader ) + ( 非路由组件如何使用history ) + ( bodyParser,cookieParser中间件 ) + ( utility MD5加密库 ) + ( nodemon自动重启node ) + +

    (1) proxy 前端的端口在:localhost:3000后端的端口在:localhost:1234所以要在webpack中配置proxy选项 (proxy是代理的意思) 在package.jso ...

  9. axios拦截器的使用方法

    很多时候我们需要在发送请求和响应数据的时候做一些页面处理,比如在请求服务器之前先判断以下用户是登录(通过token判断),或者设置请求头header,或者在请求到数据之前页面显示loading等等,还 ...

随机推荐

  1. [React Intl] Format Date and Time Using react-intl FormattedDate and FormattedTime

    Using the react-intl FormattedDate and FormattedTime components, we’ll render a JavaScript Date into ...

  2. 9、LCD驱动程序框架

    linux-3.4.2\drivers\video\S3C2410fb.c(内核自带驱动程序) fbmem.c是LCD驱动程序顶层框架文件,是一个通用的文件,在初始化init函数中会注册一个字符设备, ...

  3. UVA 10106 Product (大数相乘)

    Product The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The ...

  4. Delphi的指针(有图,很清楚)

    Pointers are like jumps, leading wildly from one part of the data structure to another. Their introd ...

  5. 分层抽样(Stratified sampling)

    1. 基本概念 统计学理论中,分层抽样针对的是对一个总体(population)进行抽样的方法.尤其适用于当总体内部,子总体(subpopulations)间差异较大时.每一个 subpopulati ...

  6. phpstorm 2018本地激活

    这是简书上找到的,害怕以后找不到,记录一下网址为https://www.jianshu.com/p/133af2e4fe3f

  7. AngularJS之ng-options指令

    1.基本下拉效果(lable for value in array) 其中select标签中的ng-model属性必须有,其值为选中的对象或属性值. <div ng-controller=&qu ...

  8. 【32.70%】【poj 2492】A Bug's Life

    Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 34687 Accepted: 11344 Description Backgr ...

  9. 【TP SRM 703 div2 500】 GCDGraph

    Problem Statement You are given four ints: n, k, x, and y. The ints n and k describe a simple undire ...

  10. STL algorithm算法make_heap和sort_heap(32)

    make_heap原型: std::make_heap default (1) template <class RandomAccessIterator> void make_heap ( ...