我有这个store/index.ts
export const storeRedux = createStore(
persistReducer(ReduxPersistConfig, rootReducer),
composeEnhancers(applyMiddleware(sagaMiddleware))
);
并具有此实现以访问组件外部的令牌
//api.ts
import axios from 'axios';
import { storeRedux } from 'store';
let token;
const listener = () => {
token = storeRedux.getState().user.token;
};
storeRedux.subscribe(listener);
// console.log(() => getAPI());
export default axios.create({
baseURL: 'http://localhost:5000/api/security',
headers: {
Authorization: `Bearer ${token}`,
},
});
如果我要添加setTimeout来包装getState()
,那么一切都会好起来的。
还有其他解决方案,因为商店已经像这样被分开了 article