我很难理解这个概念。我有2种形式可以通过相同的xhr调用来处理。因此,我正在通过公共流发送表单提交并切换到xhr调用。我正在尝试使用catchError来处理任何500个确实起作用的事(.publish(error)调用将显示一个带有错误消息的对话框),但是订阅立即完成!我以为这会一直从search $流中获取输入,直到我调用this.search$.complete()
(在componentDestroy上发生)。使用RXJS“捕捉并继续收听”时,我会缺少什么?
this.search$.pipe(
tap(() => this.pending = true),
switchMap(criteria => this._dealer.search(criteria)),
catchError(ErrorNotifierService.catchResponseErrorAsObservable)
).subscribe(response => {
this.pending = false;
if (ErrorNotifierService.isCaughtError(response)) {
this._error.publish(ErrorNotifierService.getErrorNotification(response.error));
} else {
this.dsSearchResults.data = response;
}
}, () => console.log('subscribe errored'), () => console.log('subscribe completed?'));
答案 0 :(得分:1)
您需要将catchError
移至switchMap
调用中,以“隐藏”已切换的可观察对象中的错误。只需嵌套即可:
this.search$.pipe(
tap(() => this.pending = true),
switchMap(criteria => this._dealer.search(criteria)
.pipe(catchError(ErrorNotifierService.catchResponseErrorAsObservable))
)
).subscribe(…);
您的代码存在的问题是,在您的情况下,switchMap
将切换到可观察的对象,并发出错误,从而终止订阅。我们想将此错误映射到发射之前的 ,以便切换到可观察的位置,以使生成的流甚至不会看到错误。