博客
关于我
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中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>