NgRx 效果 - 映射操作以进行顺序服务调用和更新存储

NgRx effects - mapping actions to make sequential service calls and updating store

我想对不同的实体连续传递相同的动作 2 次,例如我想添加 2 个实体,

this.store.dispatch(new Add(entity1));
this.store.dispatch(new Add(entity2));

我遇到的问题是我只得到一个动作的结果(实体2一个) 这就是我行动的结果。我想等第一个动作的结果再通过第二个动作

@Effect()
add$ = this.actions$
  .ofType<Add>(ADD)
  .pipe(switchMap(a =>
    this.sharedService.add(a.entity, a.api)
      .pipe(map(s => new AddResult(Ok(s.id))))
  .pipe(catchError(e => of(new AddResult(Err(e)))))));

您可能想要使用 mergeMap 而不是 switchMap。这是地图及其行为的列表:

@effect()
add$ = this.actions$
.ofType<Add>(ADD)
.pipe(mergeMap(a =>
this.sharedService.add(a.entity, a.api)
.pipe(map(s => new AddResult(Ok(s.id))))
.pipe(catchError(e => of(new AddResult(Err(e)))))));