0

我正在尝试自动向所有 axios 请求添加访问令牌,并且我正在使用(或尝试)中间件。但我收到以下错误:'store' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

存储/索引.ts

import { configureStore } from '@reduxjs/toolkit';
import { routerMiddleware } from 'connected-react-router/immutable';
import { createBrowserHistory } from 'history';
import { setIsAxiosAuthorized } from './middlewares';
import createRootReducer from './reducers';

export const history = createBrowserHistory();

export const store = configureStore({
  reducer: createRootReducer(history),
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware().concat(
      routerMiddleware(history),
      setIsAxiosAuthorized
    ),
});

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;

export type AppDispatch = typeof store.dispatch;

中间件.ts

import { authManager } from 'src/features/auth/authManager';
import { authActions } from 'src/features/auth/redux';
import { RootState } from '.';

export const setIsAxiosAuthorized = (store: RootState) => (next) => (
  action
) => {
  authManager.setAxiosAuthorizationToken();
  store.dispatch(authActions.setIsAxiosAuthorized(true));
  return next(action);
};

我的中间件不正确吗?我怎样才能输入它(我尝试了 StackOverflow 的不同方法,但它们导致不同的错误)?

4

0 回答 0