如何在 React 中禁用 1 个请求的拦截器?

问题描述 投票:0回答:1

我在 src 文件下有 Interceptors.js 文件。我将其导入到我需要的文件中。但它也适用于 1 个请求,即使我不导入它,我也不想使用拦截器。如何禁用此请求的拦截器?

reactjs axios interceptor
1个回答
1
投票

您的问题没有一些重要信息,但我会猜测并说您的意思是您不希望 Axios 拦截器针对某些请求运行。

如果是这种情况,您可以在拦截器代码的开头测试正在调用的请求是否不是您想要跳过拦截器的请求。像这样的东西:

axiosInstance.interceptors.request.use(async (config) => {
  if (!config.url?.includes('/your-endpoint-here')) { // <----- Here you test for the endpoint you want to skip
    // Here you do whatever should be done for all the other requests.
  }
  return config;
});

我已经这样做了,为所有 API 请求添加一些标头(一个除外)。

© www.soinside.com 2019 - 2024. All rights reserved.