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)()
}
}
vue axios拦截器加全局loading的更多相关文章
- vue axios拦截器 + 自编写插件 实现全局 loading 效果;
项目需求:用自定义的 .gif 图标实现全局 loading 效果:为避免在每个页面手动添加,且简单高效的实现,经查阅资料,最终采用了 vue axios拦截器 + 自编写 loading 插件:下面 ...
- vue --- axios拦截器+form格式请求体
在vue2.x中使用CLI生成的模板有很大改变,需要自己手动在main.ts同级目录下新建interceptors.ts interceptors.ts import axios from 'axio ...
- vue axios 拦截器
前言 项目中需要验证登录用户身份是否过期,是否有权限进行操作,所以需要根据后台返回不同的状态码进行判断. 第一次使用拦截器,文章中如有不对的地方还请各位大佬帮忙指正谢谢. 正文 axios的拦截器分为 ...
- vue axios拦截器的封装
// request.js import axios from 'axios' import qs from 'qs' // 创建axios实例 const service = axios.creat ...
- vue axios拦截器介绍
关于axios的拦截器是一个作用非常大,非常好用的东西.分为请求拦截器和响应拦截器两种.我一般把拦截器写在main.js里. 1. 请求拦截器 请求拦截器的作用是在请求发送前进行一些操作,例如在每个请 ...
- Vue基于vuex、axios拦截器实现loading效果及axios的安装配置
准备 利用vue-cli脚手架创建项目 进入项目安装vuex.axios(npm install vuex,npm install axios) axios配置 项目中安装axios模块(npm in ...
- vue导航守卫和axios拦截器的区别
在Vue项目中,有两种用户登录状态判断并处理的情况,分别为:导航守卫和axios拦截器. 一.什么是导航守卫? vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.(在路由跳转时 ...
- 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 ...
- axios拦截器的使用方法
很多时候我们需要在发送请求和响应数据的时候做一些页面处理,比如在请求服务器之前先判断以下用户是登录(通过token判断),或者设置请求头header,或者在请求到数据之前页面显示loading等等,还 ...
随机推荐
- jQuery笔记---选择器
查找API,jQuery选择器,定位标签 1.基本选择器 id定位标签 class属性定位标签 标签名定位标签 2.举例 <html> <head> <meta http ...
- python的sorted() 函数
摘自:http://www.runoob.com/python/python-func-sorted.html sortedClassCount=sorted(classCount.items(),k ...
- 设置cell背景色和选中色
// 设置cell的背景色 UIView *bg = [[[UIView alloc] init] autorelease]; bg.backgroundColor = [UIColor colorW ...
- Snail—UI学习之自己定义标签栏UITabBarController
这里的背景跟上面的差点儿相同 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkF ...
- android获取一个用于打开Word文件的intent
近期在做项目使用webview显示后,有写文档须要打开,找了一些资料,研究了一下,弄出来了! 以下贴一下主要代码: param为文档的主要路径 public static Intent getWord ...
- excel-vlookup (跨文件引用)
- Vue源码--解读vue响应式原理
原文链接:https://geniuspeng.github.io/2018/01/05/vue-reactivity/ Vue的官方说明里有深入响应式原理这一节.在此官方也提到过: 当你把一个普通的 ...
- iOS开发RunLoop学习:四:RunLoop的应用和RunLoop的面试题
一:RunLoop的应用 #import "ViewController.h" @interface ViewController () /** 注释 */ @property ( ...
- iOS开发Quartz2D十二:手势解锁实例
一:效果如图: 二:代码: #import "ClockView.h" @interface ClockView() /** 存放的都是当前选中的按钮 */ @property ( ...
- css3-8 内外边距中的注意要点有哪些
css3-8 内外边距中的注意要点有哪些 一.总结 一句话总结:padding,border都是外延的.margin会合并. 1.两元素样式都有margin:15px,他们中间的距离是15px还是30 ...