【问题标题】:Rxjs - How to call two httpService after combinelatest?Rxjs - 如何在 combinelatest 之后调用两个 httpService?
【发布时间】:2021-05-09 04:09:32
【问题描述】:

我不知道这是否是更好的解决方案,但我需要一个订阅两个 ngrx 选择器并将其用作两个 http 服务的参数的函数,我使用了 combineLatest:

  combineLatest([this.selectedCompany$, this.account$]).subscribe(res => {
  this.idCompany = res[0]!.id;
  this.account = res[1];
  this.getDashboardCardService(this.account!.id.toString(), this.idCompany.toString(), 'time');
  this.getavailableSpaceService(this.idCompany).subscribe(res => {
      this.space = res;
  })

}).unsubscribe();

我使用 combineLatest 订阅两个 ngrx 选择器,在订阅中我需要使用这些值来执行两个 http 调用 getDashboardCard 和 available。它有效,但我不喜欢它......这是更好的解决方案吗?

【问题讨论】:

    标签: javascript angular typescript rxjs ngrx


    【解决方案1】:

    简而言之,您应该在 combineLatest 之后创建 .pipe( 并在 pipe 内部使用例如 switchMap 以便能够调用另一个服务。

    【讨论】:

      【解决方案2】:

      也许你可以用它来引导你走向正确的方向?

      这里我使用了高阶可观察运算符 (concatMap);

      combineLatest([this.selectedCompany$, this.account$]).pipe(
        concatMap(([company, account]) => forkJoin([
          this.getDashboardCardService(
            account!.id.toString(), 
            company!.id.toString(), 
            'time'
          ),
          this.getavailableSpaceService(company!.id)
        ]))
      ).subscribe(([res1, res2]) => {
        this.space = res2;
      });
      

      【讨论】:

        猜你喜欢
        • 2017-06-25
        • 1970-01-01
        • 2018-04-18
        • 1970-01-01
        • 2020-02-25
        • 2021-02-21
        • 2019-01-08
        • 2019-03-03
        • 1970-01-01
        相关资源
        最近更新 更多