如何同时订阅两个可观察项,并根据第一个可观察项的结果使用第二个可观察参数?

时间:2019-07-30 13:09:37

标签: angular

我有两项服务,并且我需要从每项服务中获得一项可观察到的信息。我在互联网上进行搜索,找到了一个解决方案,位于这里:Is it good way to call subscribe inside subscribe? 但这对我不起作用。 单独调用这两个可观察的效果很好。

export class EditVarianteComponent implements OnInit {

  public currentVariante: Variante;
  public url: string;
  public value$: any;

  constructor(
    private router: Router, 
    private activatedRoute: ActivatedRoute, 
    private varService: VarianteService, 
    private famService: FamillesService
  ) {}

  ngOnInit() {
    this.url = atob(this.activatedRoute.snapshot.params.id);
    //Here, I get the resource, and I want the result of this resource to be the parameter of my second observable
    this.varService.getResource(this.url)
      .pipe(
          flatMap((res1) => this.famService.getFamilleById(res1.id_famille))
      )
      .subscribe((res3) => {
          console.log(res3);
      });
  }

1 个答案:

答案 0 :(得分:0)

您可以尝试这样:

import { map } from "rxjs/operators";

this.url = atob(this.activatedRoute.snapshot.params.id);


 this.varService.getResource(this.url).pipe(
  mergeMap(res1 => this.famService.getFamilleById(res1.id_famille))
);