vue token过期后自动跳转到登录页
作者:互联网
当token过期后,自动跳转到login页
import router from '@/router';
import axios from 'axios';
//新建个axios对象
const httpClient = axios.create({
validateStatus(status) {
return status >= 200 && status < 504 // 设置默认的合法的状态
},
timeout: 10000 //超时时间10秒
});
httpClient.defaults.retry = 3 // 请求重试次数
httpClient.defaults.retryDelay = 1000 // 请求重试时间间隔
httpClient.defaults.shouldRetry = true // 是否重试
//添加响应拦截器
httpClient.interceptors.response.use(
response => {
if (response.status !== 200) {
if (response.status === 401){
console.log(response.status)
localStorage.removeItem('authorization')
router.replace({
path: '/login'
})
}
return Promise.reject(response.data)
} else {
return response.data
}
},error => {
return Promise.reject(error)
}
);
标签:status,vue,return,token,axios,defaults,跳转,response,httpClient 来源: https://www.cnblogs.com/aiverhua/p/16478571.html