catchError后合并管道死亡



我尝试使用Angular材质分页器和排序,并从material.Angular.io示例中获得一些代码。本部分:

ngOnInit() {
this.exampleDatabase = new ExampleHttpDao(this.http);
// If the user changes the sort order, reset back to the first page.
this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);
merge(this.sort.sortChange, this.paginator.page)
.pipe(
startWith({}),
switchMap(() => {
this.isLoadingResults = true;
return this.exampleDatabase!.getRepoIssues(
this.sort.active, this.sort.direction, this.paginator.pageIndex);
}),
map(data => {
// Flip flag to show that loading has finished.
this.isLoadingResults = false;
this.isRateLimitReached = false;
this.resultsLength = data.total_count;
return data.items;
}),
catchError(() => {
this.isLoadingResults = false;
// Catch if the GitHub API has reached its rate limit. Return empty data.
this.isRateLimitReached = true;
return observableOf([]);
})
).subscribe(data => this.data = data);
}

当服务器返回错误并由catchError处理时,排序ang分页stopt以向服务器发送请求。他们的例子出了什么问题?

这就是可观测的工作方式,在onComplete或onError的情况下,可观测将停止。如果你想恢复它,你可以在catchError之后添加一个重复运算符,你的源observable是一个热Observalbe,所以你不必担心自动无限循环。或者,您可以查看repeatWhen操作员

merge(this.sort.sortChange, this.paginator.page)
.pipe(
startWith({}),
switchMap(() => {
this.isLoadingResults = true;
return this.exampleDatabase!.getRepoIssues(
this.sort.active, this.sort.direction, 
this.paginator.pageIndex).pipe(
map(data => {
// Flip flag to show that loading has finished.
this.isLoadingResults = false;
this.isRateLimitReached = false;
this.resultsLength = data.total_count;
return data.items;
}),
catchError(() => {
this.isLoadingResults = false;
this.isRateLimitReached = true;
return observableOf([]);
})
);
})
).subscribe(data => this.data = data)

或者,您可以从switchMap中捕获错误,这不应该杀死合并错误。

switchMap(() => {
this.isLoadingResults = true;
return this.exampleDatabase!.getRepoIssues(
this.sort.active, this.sort.direction, this.paginator.pageIndex).pipe(
catchError(() => {
this.isLoadingResults = false;
this.isRateLimitReached = true;
return observableOf([]);
})
);
}),

最新更新