在完成所有数据之前,正在加载Angular Page

时间:2019-05-22 17:53:27

标签: angular typescript components

我在此频道上发布了几天前遇到的一个问题,但我仍然遇到相同的问题。

我目前正在研究Angular和Typescript应用程序。

我正在进行四个不同的api调用,获取数据并将其呈现到UI页面。

此功能

public async getData(){
    let data = await this.http.get<any[]>("google.com").toPromise();
    return data;
}

从第一个Api获取数据并将其呈现到UI上,如果我仅调用第一个api,它可以很好地工作。我之所以按顺序需要它的原因是,第一个调用需要在秒功能启动之前获取特定数据,并且它需要该数据来启动自己的进程,与第三和第四功能相同。

第二个功能

 async getValueWithAsync(){
    let data = await this.http.get<any[]>("google.com").toPromise();
    return data;
}

正在从第二个Api获取第二个数据,我从这样的公共异步getData()调用它。

const a = await this.getValueWithAsync().then(res => {alert(res)});

由于某种原因,数据正在返回,我可以使用alert(res)看到它,但是每当我调用getValueWithAsync()时,getData()函数都将返回没有数据的数据,而如果我不调用它,它将附带数据,但是getData()所需的值未定义,这很有意义。

我将再次感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试一下。

this.getData().then(res => {
    alert(res);
    this.getValueWithAsync().then(res => {
        alert(res)
    });
});

注意:如果不行,请尝试使用可观察的