gpt4 book ai didi

angular - 在 RxJS 6 和 Angular 6 出现错误后如何保持可观察性

转载 作者:行者123 更新时间:2023-12-01 07:23:31 28 4
gpt4 key购买 nike

任何人都可以帮助解决 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com