我想在我的 Angular 应用程序中为批量 HTTP 请求设置一个进度计数器。我使用 forkJoin 来执行 Observables 数组。管道只执行一次。对于 obs 变量中的 X 个请求,它不会执行。在 forkJoin 中获取完整的 Observable 是否有其他方法?
这是我尝试过的:
let obs: Observable<any>[] = [...];
let counter: number = 0;
// obs has 5 items here.
forkJoin(obs)
.pipe(
tap(() => {
counter++;
})
)
.subscribe(res => {
})
.add(() => {
loadingRef.close(loadingRef);
this.refresh();
// Counter only equals 1 here.
// It should equal 5.
});
最佳答案
您可以通过添加管道来转换可观察对象,如下所示:
forkJoin(this.obs.map(o => o.pipe(tap(() => this.count++))))
.subscribe();
好吧,计数的过程非常颠簸,但这是一种解决方案
关于Angular RXJS forkJoin完成进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58328087/