Angular RXJS forkJoin 完成进度 [英] Angular RXJS forkJoin completion progress

查看:32
本文介绍了Angular RXJS forkJoin 完成进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的 Angular 应用程序中有一个用于批量 HTTP 请求的进度计数器.我使用 forkJoin 来执行一组 Observable.管道只执行一次.它不会针对 obs 变量中的 X 个请求执行.在 forkJoin 中获得完整的 Observable 是否有不同的方法?

这是我尝试过的:

 let obs: Observable[] = [...];让计数器:数字= 0;//obs 这里有 5 个项目.forkJoin(obs).管道(点按(() => {计数器++;})).subscribe(res => {}).add(() => {loadingRef.close(loadingRef);this.refresh();//这里计数器只等于 1.//它应该等于 5.});

解决方案

您可以通过添加管道来转换 observables,如下所示:

forkJoin(this.obs.map(o => o.pipe(tap(() => this.count++)))).订阅();

好吧,计数是一个非常颠簸的旅程,但这是一个解决方案

参见stackblitz

I would like to have a progress counter for bulk HTTP requests in my Angular app. I use forkJoin to execute an array of Observables. The pipe only executes once. It does not execute for the X number of requests in the obs variable. Is there a different way to get a complete Observable in a forkJoin?

Here is what I have tried:

    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.
    });

解决方案

You can transform the observables by adding a pipe, like this:

forkJoin(this.obs.map(o => o.pipe(tap(() => this.count++))))
    .subscribe();

Well, the count goes for a very bumpy ride, but it's one solution

See the stackblitz

这篇关于Angular RXJS forkJoin 完成进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆