博客
关于我
axios 全局拦截
阅读量:523 次
发布时间:2019-03-08

本文共 1628 字,大约阅读时间需要 5 分钟。

import axios from 'axios'import {    getToken }  from './auth'import {    MessageBox, Message } from 'element-ui'import store from '@/store'const service = axios.create({     baseURL: process.env.VUE_APP_BASE_API, // 接口域名地址  timeout: 5000 						// 请求超时时间})//请求拦截器(interceptors.requst)是指可以拦截每次或指定HTTP请求,并可修改配置项service.interceptors.request.use(  config => {       if (getToken()) {         config.headers['X-Token'] = getToken()// 'token'  token是在登录成功的时候才会给你 有的话加到请求头上    }      return config  },  error => {       console.log(error) // for debug    return Promise.reject(error)  })//响应拦截器(interceptors.response)可以在每次HTTP请求后拦截住每次或指定HTTP请求,并可修改返回结果项service.interceptors.response.use(  response => {       const res = response.data    if (res.code !== 20000) {         Message({           message: res.message || 'Error',        type: 'error',        duration: 5 * 1000      })      if (res.code === 50008 || res.code === 50012 || res.code === 50014) {           MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {             confirmButtonText: 'Re-Login',          cancelButtonText: 'Cancel',          type: 'warning'        }).then(() => {             store.dispatch('user/resetToken').then(() => {               location.reload()          })        })      }      return Promise.reject(new Error(res.message || 'Error'))    } else {         return res    }  },  error => {       console.log('err' + error)     Message({         message: error.message,      type: 'error',      duration: 5 * 1000    })    return Promise.reject(error)  })export default service

转载地址:http://kvanz.baihongyu.com/

你可能感兴趣的文章
mysql索引能重复吗_mysql “索引”能重复吗?“唯一索引”与“索引”区别是什么?...
查看>>
Mysql索引(4):索引语法
查看>>
mysql级联删除_Mysql笔记系列,DQL基础复习,Mysql的约束与范式
查看>>
mysql经常使用命令
查看>>
MySQL经常使用技巧
查看>>
mysql给账号授权相关功能 | 表、视图等
查看>>
MySQL缓存使用率超过80%的解决方法
查看>>
Mysql缓存调优的基本知识(附Demo)
查看>>
mysql网站打开慢问题排查&数据库优化
查看>>
mysql网络部分代码
查看>>
mysql联合索引的最左前缀匹配原则
查看>>
mysql自动化同步校验_Shell: 分享MySQL数据同步+主从复制自动化脚本_20190313_七侠镇莫尛貝...
查看>>
mysql自增id超大问题查询
查看>>
MySQL自带information_schema数据库使用
查看>>
MySQL获取分组后的TOP 1和TOP N记录
查看>>
mysql虚拟列表_动态网页制作-官方版合集下载-多特
查看>>
MySQL蜜罐反制获取攻击者信息
查看>>
Mysql表创建外键报错
查看>>
mysql表格调取数据库信息_MySQL™ 参考手册(获取有关数据库和表的信息)
查看>>
mysql表检查分析优化
查看>>