在效果中执行另一个动作后分派一个动作

时间:2019-07-02 06:53:12

标签: redux ngrx

我的代码中具有以下功能

    private loadDrones() {
    this.store$.dispatch(new UsedDronesStoreActions.GetUsedDronesRequest({organizations_id : environment.organizationId}));
    this.store$.pipe(select(UsedDronesStoreSelectors.selectAll)).subscribe((drones) => {
      drones.forEach((drone) => {
        if (this.drones.has(drone.id)) { return; }
        this.drones.set(drone.id, drone);
        this.store$.dispatch(new UsedDronesStoreActions.OpenUsedDroneUpdatePositionChannelRequest({ droneId: drone.id, projectId : environment.projectId }));
        this.store$.dispatch(new UsedDronesStoreActions.OpenUsedDroneUpdateStatusChannelRequest({ droneId: drone.id, projectId : environment.projectId }));
      });
    });
  }

我想将此功能移到INIT Action中。

使用

  @Effect()
  init$ = this.actions$.pipe(
    ofType(ROOT_EFFECTS_INIT),
    map(action => ...)
  );

我的问题是,实际的函数将加载一个列表,然后感谢该列表的结果,为该列表的每个元素调度一系列动作。

是否可以将其设为单一效果?还是我必须将其分为两个不同的动作?

1 个答案:

答案 0 :(得分:1)

https://medium.com/@amcdnl/dispatching-multiple-actions-from-ngrx-effects-c1447ceb6b22

@Effect() save = this.update$.pipe(
   map(action => action.payload),
   switchMap(payload => this.myService.save(payload)),
   switchMap(res => [
       new Notification('save success'),
       new SaveSuccess(res)
   ])
);