Javascript Axios拦截器拦截所有Axios请求

Javascript Axios拦截器拦截所有Axios请求,javascript,axios,Javascript,Axios,我使用Axios拦截器拦截我的Axios登录请求。但其他axios请求也在使用它。拥有拦截器是否意味着它将被所有axios请求使用?如果是,我如何仅将其用于特定请求。在这种情况下,我的登录请求 Axios.interceptors.response.use(res=>{ //log(`complete response-->${JSON.stringify(res)}`) //log(`这是登录响应-->${JSON.stringify(res.headers)}`) const author

我使用Axios拦截器拦截我的Axios登录请求。但其他axios请求也在使用它。拥有拦截器是否意味着它将被所有axios请求使用?如果是,我如何仅将其用于特定请求。在这种情况下,我的登录请求

Axios.interceptors.response.use(res=>{
//log(`complete response-->${JSON.stringify(res)}`)
//log(`这是登录响应-->${JSON.stringify(res.headers)}`)
const authorization=res.headers.authorization;
log(`injecting login`);
const bearerToken=authorization.substring(7,authorization.length);
常量userLoginResponse:LoginResponse={
httpStatus:res.status,
令牌:Bealertoken,
用户:{
.
.
.
}
}
this.authParams={
.
.
.
.
}
//log(`这是userLoginResponse-->${JSON.stringify(userLoginResponse)}`)
这个.setObject();
解析(userLoginResponse)
返回res;
},(错误)=>{
console.log(`这是错误状态-->${error.response.status}`)
if(error.response.status==401){
解决(错误。响应);
}
})
等待Axios.post(loginAPIURL、params、config)
拥有拦截器是否意味着它将被所有axios请求使用

如何仅将其用于特定请求

我建议为每种使用创建单独的Axios实例

比如说

const myAuthenticatedApiClient=axios.create({
baseURL:process.env.REACT\u APP\u API\u任意\u URL
})
const myOtherApiClient=axios.create({
baseURL:process.env.REACT\u APP\u API\u ADD\u INITIATIVE\u URL
})
myAuthenticatedApiClient.interceptors.response.use(res=>{
//随便
})
myAuthenticatedApiClient.post(…)//将使用拦截器
myOtherApiClient.post(“,initiative,config)//此文件上没有拦截器

您还可以将默认的
axios
实例用于未经身份验证的请求。

这些
resolve()
/
reject()
调用有什么用?看起来您已经陷入了“是”的境地,拦截器是将为所有请求调用的中间件-如果请求对象不符合您的条件(即通过/不执行任何操作),请提前返回请求对象,或者如果您只希望转换特定请求,请在
然后
之后
axios.post(…)
谢谢@Emissary中执行。成功了!!!谢谢这比在拦截器中使用条件更好、更干净