[英]Angular 4. Error in XHR request stops a chained observable
我在Angular 4中有一个可观察到的输入,当检测到URL调用时,服务会启动xhr请求。
Observable.fromEvent(this._elementRef.nativeElement, 'input')
.debounceTime(500)
.map((res: any) => { return res.target.value })
.distinctUntilChanged()
.map((res: string) => String(res.match(this.regExpUrl)))
.distinctUntilChanged()
.filter(res => res ? true : false)
.switchMap(this.serviceThatReturnAObservableOfAXHRRequest))
.subscribe(response => console.log(response),
err => console.log(err));
当xhr返回200时,可观察到的输入保持工作,我可以在输入中写入其他url,但是当返回错误时,可观察到的输入停止工作,并且如果我在输入中写了一些,则永远不会通过再次观察到。
知道为什么请求引发的错误和订阅中捕获的错误会破坏输入的可观察性,何时成功则否?
这是正确的行为。 例如,您可以使用catch(() => Observable.of())
来忽略错误,或者使用catch(err => Observable.of(err))
将其转换为next
项目,或者使用retry()
自动重新订阅源Observable。
由于您使用的是Angular,因此http
服务会自动将所有非2XX
变成错误。
类似的问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.