【问题标题】:NGRX Effect not filtering action / functions inside pipe are executedNGRX Effect 不过滤操作/管道内的功能被执行
【发布时间】:2018-08-19 22:22:09
【问题描述】:

我有一个效果,但是它里面的第一个 switchMap 之后的 mergeMap 被触发而没有触发 GetPackageCalendar 类型的动作

@Effect()
  loadCalendar$ = this.actions$
    .pipe(
      ofType(fromActions.GetPackageCalendar),
      switchMap(() => this.store.select(selectRouterState)),
      mergeMap(
        (state: RouterStateUrl) => this.store.select(selectCalendarParams).pipe(
          switchMap((calParams) => this.getPackageCalendar(state, calParams)),
          //this gets executed
        ),
      ),
      map((calendar: CalendarRS) => new fromActions.SetPackageCalendar(calendar)),
    );

`

发生了什么?

我想从存储中选择两个值并将它们作为参数传递给getPackageCalendar

【问题讨论】:

    标签: angular rxjs ngrx


    【解决方案1】:

    我的猜测是因为您的第一个 switchMap 中的 store select 部分。它在第一次通话后继续订阅GetPackageCalendar。在这种情况下,您只想选择一次存储值。所以在执行后包含一个take(1) 来自动取消订阅。

    @Effect()
    loadCalendar$ = this.actions$
      .pipe(
        ofType(fromActions.GetPackageCalendar),
        switchMap(() => {
          // take one only
          const obs$ = this.store.select(selectRouterState).pipe(take(1));
          return obs$;
        }),
        ...
      );
    

    【讨论】:

    • 仍然使用这种方法执行
    • 你能分享你的 redux 日志吗?
    • 也是你的代码。很高兴在 stackblitz.com 上有一个有效的演示来解决问题
    【解决方案2】:

    我想从存储中选择两个值并将它们作为参数传递给 getPackageCalendar

    为此使用 withLatestFrom

    @Effect()
    loadCalendar$ = this.actions$.pipe(
      ofType(fromActions.GetPackageCalendar),
      withLatestFrom(
        this.store.select(selectRouterState),
        this.store.select(selectCalendarParams)
      ),
      mergeMap(([state, calParams]) => this.getPackageCalendar(state, calParams)),
      map((calendar: CalendarRS) => new fromActions.SetPackageCalendar(calendar))
    );
    

    【讨论】:

      猜你喜欢
      • 2022-07-01
      • 2022-12-18
      • 1970-01-01
      • 2014-03-15
      • 2021-07-21
      • 2023-02-16
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多