如何使用不同的策略执行多个调用和订阅?

问题描述 投票:0回答:1

假设我有如下的代码。

this.sharedService.getvalorMensalObservable().pipe(
  switchMap(x => {
    this.valorMensal = x;
    return this.simuladorGvService.obterTaxasRecebaRapido(this.valorMensal);
    //instead of returning, I wish to make more service calls and then more than one subscribe

  })
).subscribe(response => {
  this.taxas = response;
  if (this.taxas != null) {
    this.taxasModificadas = true;
  }
});

我需要一个很好的例子来说明如何进行多管道服务调用,所以我希望进行更多的调用和更多的订阅操作,而不是从一个服务返回,然后执行一个订阅。事实上,如果有不止一种方法,我想知道每一种可能的好方法......Promise,任何地图结构(ConcatMap?这里真的很欢迎举例

angular subscribe switchmap
1个回答
0
投票

有多个操作符可以将一个observable映射到另一个。每个都有其独特的用例。这一切都取决于观测值之间如何共同依赖。我推荐你去看看 此处 来了解常见的映射策略的区别。

关于你发布的代码,你可以继续管道运算符进行多次调用。

this.sharedService.getvalorMensalObservable().pipe(
  switchMap(x => {
    this.valorMensal = x;
    return this.simuladorGvService.obterTaxasRecebaRapido(this.valorMensal);
  }),
  switchMap(x => this.otherService.transformData(x)),
  switchMap(x => this.someotherService.transformData(x)),
  .
  .
).subscribe(response => {
  this.taxas = response;
  if (this.taxas != null) {
    this.taxasModificadas = true;
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.