在main.js的 new vue之前加入:
axios.interceptors.request.use(标签:username,axios,请求,token,拦截器,config,authorization From: https://www.cnblogs.com/lytcreate/p/17618192.html
config => {
// 在发送请求前,获取新的 token
var username = window.localStorage.getItem('username');
var authorization = window.localStorage.getItem('token');
console.log(username)
console.log(authorization)
if (username && authorization) {
// 如果这两个字段存在,则添加到请求头
config.headers.authorization = authorization;
config.headers.username = username;
}
return config;
},
error => {
return Promise.reject(error);
}
);