我有一个角度应用程序,我想要一个副作用调用服务到第三方分析平台。我的想法是做
Any action fires -> Side effect catches everything -> Service to call analytics话虽如此,我显然不想在每个效果中都添加这个流程。我只希望在树的顶部有一个" catch - all“的副作用来捕获任何和所有的Ngrx操作,而不是调度一个操作,而只是调用服务。我在语法上有点问题...
@Injectable()
export class AmplitudeEffects {
constructor(private actions$: Actions) {}
@Effect()
private *any action here* = this.actions$.pipe( <--what to put here
ofType(), <-- leave empty to catch everything?
mergeMap(() =>
this.amplitudeService.sendValues(arg1, arg2, arg3, arg4).pipe(
// catchError(error => of(new AmplitudeFailure(error)))
)
)
);
}发布于 2019-07-30 02:49:21
这是一个很好的特效用例,我也用Start using ngrx/effects for this给出了这个例子。
为了回答您的问题,您可以省略ofType:
@Effect()
log = this.actions$.pipe(
mergeMap(() =>
this.amplitudeService.sendValues(arg1, arg2, arg3, arg4).pipe(
// catchError(error => of(new AmplitudeFailure(error)))
)
)
);我不确定您是否希望捕获错误,因为这只是为了记录日志,所以您可以这样做:
@Effect({ dispatch: false })
log = this.actions$.pipe(
mergeMap(() =>
this.amplitudeService.sendValues(arg1, arg2, arg3, arg4)
)
);发布于 2019-07-30 02:49:21
只需删除ofType,您的错误处理将终止可观察对象,因此ngrx将停止工作,因此我添加了正确的方法来处理catchError。我看起来应该是这样的,但是因为我不知道sendValues做了什么,所以我认为它会返回一个可观察对象。
@Effect()
name = this.actions$.pipe(
this.amplitudeService.sendValues(arg1, arg2, arg3, arg4).pipe(
map((x: any)=> x),
catchError((error: any, effect: Observable<Action>) => {
return effect.pipe(startWith(new new AmplitudeFailure(error)));
}
)
)
);https://stackoverflow.com/questions/57259260
复制相似问题