gpt4 book ai didi

angular - catchError 后合并管道死亡

转载 作者:行者123 更新时间:2023-12-05 02:14:27 26 4
gpt4 key购买 nike

我尝试使用 Angular Material 分页器和排序,并从 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处理它时,排序和分页停止向服务器发送请求。他们的例子有什么问题?

最佳答案

这就是 observable 的工作方式,在 onComplete 或 onError 的情况下,observable 会停止。如果你想让它恢复,你可以在 catchError 之后添加一个 repeat 运算符,你的源 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)

关于angular - catchError 后合并管道死亡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53504241/

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