微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

处理 Angular II 中的异步和可观察对象的问题

如何解决处理 Angular II 中的异步和可观察对象的问题

在我的项目中,我需要根据某些日期(月和年)计算一些数据,所以一旦我在数组“this.etiquetasEjeX”中获得了日期,我需要循环它们来计算数据。

数据是一些数组,然后将成为chart.js图形的数据源,因此这些数组是我的类的属性

private etiquetasEjeX: string[] = [];
private contratadoPrevisto: number[] = [];
private certificadoRealizado: number[] = [];
private certificadoPrevisto: number[] = [];
private gastadoRealizado: number[] = [];
private gastadoPrevisto: number[] = [];
private gastadoPlanificado: number[] = [];
private certificadoPlanificado: number[] = [];

我的组件中有这个

ngOnInit() {
 this.crearEtiquetasEjeX();
 this.getGastadoRealizado();
 this.getCertificadoRealizado();
 this.getContratadoPrevisto();
 this.getGastadoPlanificado()
 this.getCertificadoPlanificado();
 this.getGastadoPrevisto();

}

在 this.crearEtiquetasEjeX() 中,我得到带有日期的数组来循环它们

然后在每个函数中我循环 this.etiquetasEjeX 数组来计算相关数据,例如

getGastadoPlanificado(){
 let valorAnterior:number=0;
 let valorEnMesYAño:number=0;

 const responses=this.etiquetasEjeX.map(
  etiqueta=>{
    return this.dataService.getGastoPlanificadoEnMesYAño(
     this.proyectoId,getMonthNumber(etiqueta.slice(0,etiqueta.length-4)),+etiqueta.slice(-4),this.acumular)
    .pipe(
      tap(item=>console.log(`GraficaComponent-getGastadoPlanificado() en ${etiqueta}: ${item}`))
    )
  }
)

forkJoin(responses)
.subscribe(
  items=>{
    items.forEach(
      item=>{
        this.gastadoPlanificado.push(item);
      }
    )
  }
)

}

这在他们每个人中。所以我在数组中插入值

但是在最后一个函数 this.getGastadoPrevisto() 中,我需要使用先前生成的数组进行操作,我想知道在函数内部这个数组何时为空

empty

但是,如果我不在函数内部放置任何断点,则数组已满

full

我看到函数是按顺序执行的,但在某些时候我失去了流程,即使它们在轮到 getGastadoPrevisto() 时按顺序执行,在 getgastadoPlanificado() 中填充的数组还没有完成,我不知道如何管理这个

有什么想法吗?

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。