Angular Promise don't work : ERROR TypeError: DataSource is undefined

Angular Promise don't work : ERROR TypeError: DataSource is undefined

我在等待填充数据源时遇到问题:

HTML

<app-single-y-axis [xAxysDataSource]="dataSource['labels']"
                   [yAxysDataSource]="dataSource['series']">
</app-single-y-axis>

COMPONENT.TS

ngOnInit(): void {
  this.getData();
}

getData = async () => {
  this.dataSource = await this.dataService.getDataAnalysis();
}

我也试试(但结果一样):

 getData = async () => {
     this.dataService.getDataAnalysis()
      .then (res => {
          this.dataSource = res;
      })
    })
 }

数据服务

getDataAnalysis = async () => {
  return new Promise((resolve, reject) => {
     do something...
     this.anotherService.deviceDataAnalysis()
       .then(analysisRes => { i'll do what i have to do and works}
     }
  })
}

其他服务

deviceDataAnalysisPromise() {
return this.http.post(BACKEND_URL + "url")
  .pipe(map((res: any) => {
    return res;
  })).toPromise();

问题是当我 运行 它时,在控制台日志中我有

ERROR TypeError: ctx_r31.dataSource is undefined 

参考HTML,3 / 7次,这取决于,直到数据源被填充...我不明白如何告诉他更好地等待所有人的回应,然后再开始进入恐慌...

显然,填充数据源后一切正常

有人可以帮帮我吗???

你 return 由 getDataAnalysis 做出的承诺从未得到兑现。 您可以简单地 return 您已有的 new Promise() 而不是创建 new Promise()

getDataAnalysis = async () => {
  return this.anotherService.deviceDataAnalysis()
       .then(analysisRes => { return "i'll do what i have to do and works"});
}

这样解决了

HTML

<span *ngIf="DataSourceDefined">

在我的组件中,当它接收到数据源时,我将 DataSourceDefined = true 设置为...