Ajax Axios拦截器不';页面加载时不能截取

Ajax Axios拦截器不';页面加载时不能截取,ajax,xmlhttprequest,vue.js,axios,Ajax,Xmlhttprequest,Vue.js,Axios,我在Vue应用程序中实现JWT以进行授权,并在使用令牌后刷新它们 我使用了axios拦截器,所以我可以截获到我的API后端的每个请求,这似乎可以用于登录等。。。但是,一旦我刷新页面,就会使用最后一个令牌正常发出请求 问题是axios拦截器在这一点上似乎不起作用,所以一旦令牌被使用,我就不能用新的令牌更新它 下面是我设置拦截器的方法:- window.axios.interceptors.request.use(function (config) { console.log("Sent r

我在Vue应用程序中实现JWT以进行授权,并在使用令牌后刷新它们

我使用了axios拦截器,所以我可以截获到我的API后端的每个请求,这似乎可以用于登录等。。。但是,一旦我刷新页面,就会使用最后一个令牌正常发出请求

问题是axios拦截器在这一点上似乎不起作用,所以一旦令牌被使用,我就不能用新的令牌更新它

下面是我设置拦截器的方法:-

window.axios.interceptors.request.use(function (config) {
    console.log("Sent request!");

    return config;
}, function (error) {
    console.log("Failed sending request!");

    return Promise.reject(error);
});

window.axios.interceptors.response.use(function (response) {
    console.log("Got headers:", response.headers);
    if (response.headers.hasOwnProperty('authorization')) {
        console.log("Got authorization:", response.headers.authorization);
        Store.auth.setToken(response.headers.authorization);
    }

    return response;
}, function(err){
    console.log("Got error", err);
});
我没有在页面加载中获得任何
控制台.log

我正在根应用程序的
beforeMount
方法中设置拦截器。我尝试在创建之前将它们移动到
,但仍然遇到同样的问题。

试试这个

window.axios.interceptors.request.use(function (config) {
console.log("Sent request!");

if(localStorage.getItem('id_token')!=undefined){
    config.headers['Authorization'] = 'Bearer '+localStorage.getItem('id_token')
}
return config;}  , function (error) {
console.log("Failed sending request!");

return Promise.reject(error); });