首页 > 解决方案 > 如何使用 withLatestFrom 运算符在 Effects 中获取存储状态

问题描述

我正在尝试这个

payloadToBeSaved$ = createEffect(() =>  this.actions$
.ofType(SOME_ACTION)
.withLatestFrom(this.store$)
.map(([action: Action, storeState: AppState]) => {
   // Do something ...
});

但是 this.actions$ 是一个可观察的,我必须使用 this.actions$.pipe().... 当我尝试使用 pipe() 时,所有行都是红色的错误。不知道怎么修。

标签: ngrxngrx-store

解决方案


@Effect()
shipOrder = this.actions.pipe(
  ofType<ShipOrder>(ActionTypes.ShipOrder),
  map(action => action.payload),
  concatMap(action =>
    of(action).pipe(
      withLatestFrom(store.pipe(select(getUserName)))
    )
  ),
  map([payload, username] => {
    ...
  })
)

参考:为此开始使用ngrx/effects


推荐阅读