【发布时间】:2019-07-05 19:37:30
【问题描述】:
我有一个带有 NgRx 的 Angular 应用程序,在我的一个效果中,我有一个非预期的行为,结果是预期的,但我不知道如何解决它。
我的缩小代码是
@Effect()
detelete$ = this.actions$
.pipe(
ofType<fromActions.DeleteRequested>(fromActions.ActionTypes.DeleteRequested),
map(action => action.payload.id),
mergeMap(id =>
combineLatest([
of(id),
this.rservice.deleteById(id)
.pipe(
catchError((err, caught$) => {
this.store.dispatch(new fromActions.DeleteCancelled({id: id}));
return caught$;
})
)
])
),
map(([id, ]: [string, any]) => {
return new fromActions.DeleteSuccess({id: id});
}),
);
我的catchError 与mergeMap 不在同一级别的原因是我需要id 作为fromActions.DeleteCancelled 操作的有效负载。另外,我的服务只返回一个布尔值,所以我使用combineLatest 将它持久化到我的onSuccess map。
我遇到的是这个catchError 正在执行多次。因此,多次分派我的错误操作。
我发现了
如果你返回这个源,observable 将有效地重新启动并重试
在这种情况下,source 是我的caught$。
如果,在我的cacthError 中,我返回
return of(new fromActions.DeleteCancelled({id: id}));
它仍然会转到我的onSuccess map。我也许可以检查我在map 中的第二个参数是操作系统类型Action 还是boolean,但我认为有一种正确的方法来处理它,我不知道。
StackBlitz (uncomment subscribe to see infinite loop)
谢谢。
【问题讨论】:
-
你能试试
return [new fromActions.DeleteCancelled({id: id})];吗?
标签: angular rxjs ngrx ngrx-effects