angular - 如何在 Ngrx 效果中组合多个可观察对象?
问题描述
我的 NGRX 效果执行一个 API 请求,然后分派一个新操作。在这个新操作中,我需要访问初始操作,但是在 switchMap 之后该引用丢失了:
@Effect()
public myAction$: Observable<Action> = this.actions$.pipe(
ofType<DoSomethingRequest>(MyActions.DO_SOMETHING),
switchMap((action)=> this.backendService.doSomething(action.id, action.payload),
map(dto => new DoSomethingSuccess(/* here I need both action.payload and dto */))
);
我怎样才能在我的链中保留原始动作?我需要 API 调用的操作和结果。
解决方案
您可以重组您的链,使原始操作与第二个操作在同一范围内:
@Effect()
public myAction$: Observable<Action> = this.actions$.pipe(
ofType<DoSomethingRequest>(MyActions.DO_SOMETHING),
switchMap((action)=> this.backendService.doSomething(action.id, action.payload).pipe(
map(dto => new DoSomethingSuccess(/* here I need both action.payload and dto */)),
),
);
map
在您的用例中,将switchMap
.