0

我已将自己的项目从 5 版本移至 angular 6、@angular-redux/store 9。一切都很好,但是在中间件中注册史诗的方式不起作用。我从中间件中删除了所有史诗,除了记录器,启动所有操作都很好。然后我尝试添加一个史诗并得到类似的错误

TypeError: action$.pipe is not a function
at SecurityUserEpics.loadSecurityUser (security-user.epic.ts:31)
at combineEpics.js:19

然后我尝试解决这个问题并搜索许多示例和文档,但找不到解决方案。在决定在这里提出问题并准备问题代码后,找到了解决方案。Soo,我在这里发布该解决方案,希望这可以为某人节省时间。史诗:

loadSecurityUser = (action$) => {
  debugger;
  // here i have object of type : {getState: ƒ, dispatch: ƒ} and thats was pain from older version. pipe helps!
  return action$.pipe(
     ofType(SecurityUserActions.LoadSecurityUsers),
     switchMap(q => this._dataSrv.getUserInfo()
        .pipe(map(data => {
           localStorage.setItem('isNeedRelogin', '0');
           return this._securityUserActions.loadSecurityUserComplete(data);
        }))));

}

最后的配置存储:

const epicMiddleware = createEpicMiddleware();
const middleware = [epicMiddleware];
middleware.push(createLogger());
ngRedux.configureStore(combinedReducer, INITIAL_STATE, middleware, storeEnhancers);
const rootEpic = combineEpics(this._securityUserEpics.loadSecurityUser);
// this is new way of middleware registration
epicMiddleware.run(rootEpic);
4

1 回答 1

0

使用 provideStore 代替 configureStore,例如 -

export const combinedEpics = combineEpics(
   pingEpic,
   fetchUserEpic
);

//replace ngRedux.configureStore with:
const epicMiddleware = createEpicMiddleware();

const store = createStore(
  combinedReducer,
  INITIAL_STATE,
  applyMiddleware(createLogger(), epicMiddleware),
);

ngRedux.provideStore(store);
epicMiddleware.run(combinedEpics);

有关史诗的更多信息 - https://redux-observable.js.org/docs/basics/Epics.html

于 2018-08-06T07:36:33.707 回答