1

我已经设置了这个拦截器

const setupAxiosInterceptors = (onUnauthenticated: () => void) => {
  const onRequestSuccess = (config: AxiosRequestConfig) => {
    console.log("request success", config);
    const token = Storage.local.get("auth");
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  };
  const onRequestFail = (error: AxiosError) => {
    console.log("request error", error);
    return Promise.reject(error);
  };
  axios.interceptors.request.use(onRequestSuccess, onRequestFail);

  const onResponseSuccess = (response: AxiosResponse) => {
    console.log("response success", response);
    return response;
  };
  const onResponseFail = (
    error: { status: number } & { response: { status: number } }
  ) => {
    const status = error.status || error.response.status;
    if (status === 403 || status === 401) {
      onUnauthenticated();
    }
    return Promise.reject(error);
  };
  axios.interceptors.response.use(onResponseSuccess, onResponseFail);
};
export default setupAxiosInterceptors;

这个动作使用 react 工具包

export const authLogoutEffects = () => (
  dispatch: ThunkDispatch<{}, {}, AnyAction>
) => {
  dispatch(authLogout());
  Storage.local.remove("auth");
};

然后在我的 index.ts

const { dispatch } = store;
setupAxiosInterceptors(() => {
  dispatch(authLogoutEffects());
});

store 是 configureStore react 工具包的一个实例

但我在调度参数中有一个错误

'(dispatch: ThunkDispatch<{}, {}, AnyAction>) => void' 类型的参数不可分配给 'AnyAction | LocationChangeAction'。类型 '(dispatch: ThunkDispatch<{}, {}, AnyAction>) => void' 缺少来自类型 'LocationChangeAction' 的以下属性:type, payloadts(2345)

你能帮我吗?

4

0 回答 0