Stay before every beautiful thoughts.
Just be nice, always think twice!
axios token验证拦截器
import axios from 'axios';
// req拦截
axios.interceptors.request.use(
let token = localStorage.getItem('token')
config => {
if (token === null) { // 判断是否存在token,如果存在的话,则每个http header都加上token
config.headers.Authorization = `${token}`;
}
return config;
},
err => {
return Promise.reject(err);
});
// res拦截
axios.interceptors.response.use(
response => {
return response;
},
error => {
if (error.response.code === 401) {
// 返回 401 跳转到登录页面
router.replace({
path: 'login',
query: {redirect: router.currentRoute.fullPath}
})
}
return Promise.reject(error.response.msg) // 返回接口返回的错误信息
});
PS: 建议把拦截器独立到一个js文件,然后在引入。