Angular 角度NGRX@效果捕捉所有动作
我有一个angular应用程序,我希望有一个副作用,将服务调用到第三方分析平台。我的想法是Angular 角度NGRX@效果捕捉所有动作,angular,ngrx,ngrx-effects,Angular,Ngrx,Ngrx Effects,我有一个angular应用程序,我希望有一个副作用,将服务调用到第三方分析平台。我的想法是 Any action fires -> Side effect catches everything -> Service to call analytics 话虽如此,我显然不想在每种效果中都添加这种流。我只希望在树的顶部有一个“全面覆盖”的副作用来捕获任何和所有Ngrx操作,而不是调度操作,只需调用服务即可。我的语法有问题 @Injectable() export class Ampl
Any action fires -> Side effect catches everything -> Service to call analytics
话虽如此,我显然不想在每种效果中都添加这种流。我只希望在树的顶部有一个“全面覆盖”的副作用来捕获任何和所有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)))
)
)
);
}
@Injectable()
导出类AmplitudeEffects{
构造函数(私有操作$:操作){}
@效果()
private*此处的任何操作*=此.actions$.pipe(属于(新的AmplitudeFailure(错误)))
)
)
);
}
这是一个很好的效果用例,我在中也给出了这个例子
要回答您的问题,您可以将类型的去掉:
@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)
)
);
只要删除ofType,错误处理就会终止observable,这样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)));
}
)
)
);
@Effect()
name=此.actions$.pipe(
this.amplitudeService.sendValues(arg1、arg2、arg3、arg4).pipe(
映射((x:any)=>x),
捕获错误((错误:任何,影响:可观察)=>{
返回效果。管道(起始时间(新振幅放大倍数(错误));
}
)
)
);
只需注释of type()函数就可以了。