[英]How do I return a translated message from a subscription in a catcherror?
我希望我的catch错误返回一个新的LoginFailure操作,但是带有来自翻译服务订阅的消息。 这个实现给了我:
类型'(error:any)=> void'的参数不能赋给类型'(err:any,caught:Observable)=> ObservableInput <{}>'的参数。
@Effect()
login$ = this.actions$.ofType<Login>(AuthActionTypes.Login).pipe(
map(action => action.payload),
exhaustMap(auth =>
this.authService.login(auth).pipe(
map(data => new LoginSuccess({ data: data })),
catchError(error => {
this.translateService
.get('LoginErrorMessage')
.subscribe((res: string) => {
of(new LoginFailure(res));
});
}
)
)
)
);
任何帮助,将不胜感激。
catchError
返回任何catchError
。 mergeMap()
(展平)Observable而不是Subscription。 根据您希望此请求的接收者将消息视为成功或错误,您必须返回错误或成功。
@Effect() login$ = this.actions$.ofType<Login>(AuthActionTypes.Login).pipe( map(action => action.payload), exhaustMap(auth => this.authService.login(auth).pipe( map(data => new LoginSuccess({ data: data })), catchError(error => { return this.translateService .get('LoginErrorMessage') .pipe( mergeMap((res: string) => { return of(new LoginFailure(res)); // to get it in success callback // or return throwError(new LoginFailure(res)) // to get it in error call back }); ) } ) ) ) );
如果您在获得登录错误后不想调用另一个observable,则无需使用展平运算符。 法线map()
会做。
@Effect() login$ = this.actions$.ofType<Login>(AuthActionTypes.Login).pipe( map(action => action.payload), exhaustMap(auth => this.authService.login(auth).pipe( map(data => new LoginSuccess({ data: data })), catchError(error => { return this.translateService .get('LoginErrorMessage') .pipe( map((res: string) => { if (you want the response in success callback) { return new LoginFailure(res); } else { // if you want it in error throw "your error"; // say, {error: "I am Error"} } }); ) } ) ) ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.