Angular 7/rxjs-发生捕获错误后,订阅不会';我得不到更多的价值
假设我有以下代码:Angular 7/rxjs-发生捕获错误后,订阅不会';我得不到更多的价值,angular,typescript,rxjs,Angular,Typescript,Rxjs,假设我有以下代码: this.service1 .getValues() .pipe( mergeMap(response => this.service2.getMoreValues(response.id)), catchError(err => of({})) ) .subscribe(response) => { console.log(response) }); 我的问题是:如果调用catchError,我的订阅将不再有新
this.service1
.getValues()
.pipe(
mergeMap(response => this.service2.getMoreValues(response.id)),
catchError(err => of({}))
)
.subscribe(response) => {
console.log(response)
});
我的问题是:如果调用catchError,我的订阅将不再有新的值。我想做的是:如果调用catchError,我将返回类似于空对象的内容并正常进行,仍然希望新值来自我的服务
有人能告诉我为什么在catchError被解雇后,订阅不再起作用了吗?谢谢。这是正确的行为
catchError
将订阅从其回调返回的Observable
这意味着,如果您想让它继续从源观测值发射值,您可以重新订阅它
import { concat } from 'rxjs';
const source = this.service1
.getValues()
.pipe(
mergeMap(response => this.service2.getMoreValues(response.id)),
catchError(err => concat(
of({}),
source,
)),
);
// Maybe this would work as well, but I didn't test it
// catchError((err, caught) => concat(
// of({}),
// caught,
// )),
source.subscribe(response) => {
console.log(response)
});
最后,有一个retry()
操作符可以自动为您重新订阅,但在重新订阅之前,您将无法通过({})的
我想做的是:如果调用catchError,我将返回类似于空对象的内容
尝试在catchError
中返回({})的,,如下所示:
this.service1
.getValues()
.pipe(
mergeMap(response => this.service2.getMoreValues(response.id)),
catchError(err => {
return of({});
})
)
.subscribe(response) => {
console.log(response)
});
你在mergeMapOh之后漏掉了逗号(,)。对不起,这只是一个例子。我不想验证上面的代码,我想理解为什么在触发catchError后订阅不再工作。