首页 > 解决方案 > 我需要进行两次订阅等待

问题描述

我正在通过项目的 id 获取列表,但现在要获取卡片,我需要获取列表的 id,但是为此,如果您可以指导我了解代码的方式,我需要等待

this.route.paramMap.pipe(
  map((param: ParamMap) => {
    // @ts-ignore
    return param.params.id;
  })
).subscribe(prodId => {
  this.id = prodId;
  console.log("Titulo de Proyecto", prodId)
  this.API.getNombreProyecto(this.id).subscribe(prod => {
    this.project = prod;
  });

  
});

this.API.getListas(this.id)
.subscribe((data: Customer[]) => {
  console.log('Obtener Listas:::', data);
  this.customersx = data;
  console.log('Obtener Listas:::',this.customersx);
  
}); 


this.API.getCards(this.id)

.subscribe((data: CustomerCards[]) => {
  this.defaults.id = this.id;
  console.log('Obtener Listas:::', data);
  this.customersy = data;
  console.log('Obtener Listas:::',this.customersy);
  
}); 

标签: angular

解决方案


我完全没有得到您真正想要实现的目标,因为您发布的描述似乎不容易理解。

但我会试着给你一个想法,你怎么能做到这一点。检查下面的代码。

 this.route.paramMap
  .pipe(
    switchMap((param: ParamMap) => {
      this.id = param.params.id;
      return forkJoin([
          this.API.getNombreProyecto(this.id),
          this.API.getListas(this.id),
          this.API.getCards(this.id)
      ]);
    })
  )
  .subscribe(([project, customersx, data])=>{
    this.project = project;
    this.customersx = customersx;
    this.customersy = data;
  })

现在通过引用上面的代码,您可以尝试修改您的代码以使其按您想要的顺序。


推荐阅读