通过另一个switchMap和filter传递效果给switchMap的动作值?

问题描述

一个密码

  @Effect()
  myEffect$ = this.actions$.pipe(
    ofType(MyActions.doSomething),tap(() => this.store.dispatch(MyActions.doSomething2())),switchMap(action => {
      

有效。 我需要在点击之前注入过滤器。如何做到使switchMap不会丢失操作值?

我正在尝试这样做

  @Effect()
  myEffect$ = this.actions$.pipe(
    ofType(MyActions.doSomething),switchMap(() => this.store.pipe(select(mySelectors.getAmount),take(1))),filter(amount => amount <= 0),switchMap(action => {
    ....

无论怎么说property action.xxxx doesn't exist on type number,这很清楚。如何不丢失MyActions.doSomething返回的操作值?

解决方法

如果只对那些值进行更好的封装,我将直接在金额存储的操作员链中移动过滤器。然后,您不必导出该switchMap之外的金额,只需将它们重新映射为操作即可:

  @Effect()
  myEffect$ = this.actions$.pipe(
    ofType(MyActions.doSomething),switchMap(action => this.store.pipe(
      select(mySelectors.getAmount),take(1),filter(amount => amount <= 0),// filter amounts here
      map(_ => action) // remap to actions
    ))),tap(() => this.store.dispatch(MyActions.doSomething2())),switchMap(action => {
      // ...
    })