Axios:如何正确取消请求拦截器中的请求? [英] Axios: how to cancel request inside request interceptor properly?
本文介绍了Axios:如何正确取消请求拦截器中的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果没有令牌,我想取消请求,所以我这样做:
I want to cancel the request if there's no token so I do like this:
instance.interceptors.request.use(config => {
if (!getToken()) {
console.log("interceptors: no access token");
} else {
config.headers.Authorization = "Bearer " + getToken().accessToken;
return config;
}
});
但是在负面情况下,会出现错误TypeError: Cannot read property 'cancelToken' of undefined
.
But in negative scenario there's an error TypeError: Cannot read property 'cancelToken' of undefined
.
推荐答案
您不能在拦截器中使用令牌,而是抛出Cancel
You cannot use the token inside the interceptors but instead throw Cancel
axios.interceptors.response.use(function (response) {
throw new axios.Cancel('Operation canceled by the user.');
}, function (error) {
return Promise.reject(error);
});
请参阅此帖子: https://github.com/axios/axios/issues/583
这篇关于Axios:如何正确取消请求拦截器中的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文