简体   繁体   English

Rxjs - combinelatest 后如何调用两个 httpService?

[英]Rxjs - How to call two httpService after combinelatest?

I don't know if it's the better solution but I need a function that subscribes to two ngrx selectors and use it as parameters for two http services and I used combineLatest:我不知道这是否是更好的解决方案,但我需要一个订阅两个 ngrx 选择器并将其用作两个 http 服务的参数的 function,我使用了 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();

I use combineLatest to subscribe to two ngrx selectors and in subscribe I need to use these values for doing two http calls, getDashboardCard and available.我使用 combineLatest 订阅两个 ngrx 选择器,在订阅中我需要使用这些值来执行两个 http 调用,getDashboardCard 和可用。 It works but I don't like it...which is the better solution for doing it?它有效,但我不喜欢它......这是更好的解决方案吗?

In short, You should make .pipe( after combineLatest and inside that pipe use for example switchMap to be able to call another service.简而言之,您应该在.pipe(combineLatest switchMap pipe调用另一个服务。

Maybe you can use this to get you headed the right direction?也许你可以用它来让你朝着正确的方向前进?

Here I use a higher order observable operator ( concatMap );这里我使用了更高阶的可观察运算符( 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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM