Kri*_*hna 2 rxjs angular angular6 rxjs6
任何人this._getReactions$.next()都this.http.get(...)可以在出现错误时不工作的情况下提供帮助。我想保持可观察状态以接受下一个输入。
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
});
}Run Code Online (Sandbox Code Playgroud)
onClick() {
this._getReactions$.next();
}Run Code Online (Sandbox Code Playgroud)
如果可观察的模具将其称为错误处理程序,并且它们已关闭,则您将无法通过它们发送任何消息,这意味着它们已关闭,包括间隔已死的上游所有部件均已关闭。
如果我们想生活怎么办。
搁置主要观察者链的解决方案是在
每次触发请求时将catch放入其中,switchmapswitchmap
从而创建可观察的ajax,并且这次使用
catch。
switchmap有一种行为表明我的信息来源尚未完成,所以我真的不在乎孩子是否完成了我会继续前进。
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2329 次 |
| 最近记录: |