我有以下代码,我试图重构只有一个订阅没有成功。
this.emailsService.askOptionOne(email).pipe(
takeUntil(this.ngUnsubscribe)
).subscribe(
() => {
this.isBusy = false;
assistanceForm.reset();
this.router.navigate(['/']);
this.translateService.get('The request has been sent.').subscribe((message: string) => {
this.toastrService.success(message);
});
},
(error: any) => {
this.isBusy = false;
this.translateService.get(error).subscribe((message: string) => {
this.toastrService.error(message);
});
}
);
我目前所做的努力:
this.emailsService.askOptionOne(email).pipe(
switchMap(() => of('The request has been sent.')),
catchError((error: any) => of(error)),
switchMap((message: any) => this.translateService.get(message)),
takeUntil(this.ngUnsubscribe)
).subscribe(
(message: any) => {
this.isBusy = false;
assistanceForm.reset();
this.router.navigate(['/']);
this.toastrService.success(message);
},
(error: any) => {
this.isBusy = false;
this.toastrService.error(error)
}
);
这个版本的问题是结果永远不会出错。
有重构这段代码的线索吗?
谢谢你的帮助。