【问题标题】:Angular 6 effect merge payload and data from service call in a new actionAngular 6 效果在新操作中合并来自服务调用的有效负载和数据
【发布时间】:2019-08-02 22:03:56
【问题描述】:

我从一个动作payload: { items: Administration[], id: number} 中得到一个有效载荷 效果。

我使用内部服务来发布新值并在成功操作中返回它。

我只想通过 httpCall 的结果传递 id,但在服务之后会丢失有效负载。

我在 switchMap 之后尝试了mergeMap(([action, items]) => new SuccessAction({items}))

但不太明白在哪里或如何实现它,诸如此类,

@Effect() update$: Observable<Action> = this.actions$.pipe(
  ofType<UpdateAction>(ActionTypes.UPDATE),
  switchMap(action => this.service.update(action.payload.items).pipe(
  map(response => response.items }), 
    catchError(error => of(new FailureAction({error}))
  ))),
  mergeMap(([action, items]) => new UpdateAction({action.payload.id, items}))
);`

如果有人可以帮助我。

【问题讨论】:

    标签: angular rxjs6 ngrx-effects


    【解决方案1】:

    注意新的 Rxjs 标准,您实际上是在使用管道中的管道。 问题是您正在使用在管道(有效负载)中丢失的数据,您可以将其保留在其中,也可以使用 withLatestFrom() 重新获取信息。

    @Effect() update$: Observable<Action> = this.actions$.pipe(
        ofType<UpdateAction>(ActionTypes.UPDATE),
        switchMap(action => ({ payload: of(action.payload), response: this.service.update(action.payload.items)})),
        map(data => new UpdateAction({id: data.payload.id, items: data.response.items})),
        catchError(error => of(new FailureAction(error))),
    );
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多