我已经设置了这个拦截器
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());
});
商店是configureStore react工具箱的一个实例
但是我在派遣参数中有一个错误
'(dispatch:ThunkDispatch <{},{},AnyAction>)类型的参数=> 无效”不能分配给“ AnyAction |类型”参数 LocationChangeAction”。输入'(调度: ThunkDispatch <{},{},AnyAction>)=> void'缺少以下内容 类型“ LocationChangeAction”中的属性:类型, 有效载荷(2345)
可以帮我吗?