任何人都可以帮助解决 this._getReactions$.next()
的场景每次都不工作 this.http.get(...)
得到一个错误。我想让 observable 保持事件状态以获取下一个输入。
private _getReactions$: Subject<any> = new Subject();
constructor() {
this._getReactions$
.pipe(
switchMap(() => {
return this.http.get(...)
// http request
}),
catchError(error => {
console.log(error);
return empty();
})
)
.subscribe(data => {
console.log(data)
//results handling
});
}
onClick() {
this._getReactions$.next();
}
最佳答案
如果 observable 死了,它会调用它的错误处理程序并且它们被关闭,你不能通过它们发送任何东西,这意味着它们在上游的所有东西都被关闭,包括间隔是死的。
what if we want to live.
屏蔽主观察者链是解决方案
把catch放在
switchmap
里面每当请求被触发时
switchmap
创建 ajax observable,这次使用catch
.switchmap
有一种行为表明我的消息来源还没有完成所以我真的不在乎 child
完成我会继续前进。
constructor() {
this._getReactions$
.pipe(tap(value => { this.loading = true; return value }),
switchMap(() => {
return this.http.get(...).pipe(
catchError((error) => this.handleError(error)))
// http request
}),
)
.subscribe(data => {
console.log(data)
//results handling
this.error = false;
this.loading = false
});
}
private handleError(error: HttpErrorResponse) {
this.error = true;
console.log(error)
this.loading = false
return empty();
Live Demo
Detailed Info
PS:嵌套在任何
flattening
中运算符,例如 mergeMap
, concatMap
, exhaustMap
和其他展平运算符也可以工作。
关于angular - 在 RxJS 6 和 Angular 6 出现错误后如何保持可观察性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52370506/