【发布时间】:2019-09-13 18:26:34
【问题描述】:
我需要的是从一个 api 获取数据,然后将其发送到另一个并附加值。
我尝试过使用异步等待但没有成功,我认为由于嵌套类型它不起作用
//service.ts
getRecentBackups();
{
return this.http.get<any[]>(
`${environment.URL}`
);
}
getAccounts(accountId);
{
return this.http.get(
`${environment.URL}/${accountId}`
);
}
//component.ts
this.abc.getRecentBackups().subscribe(data => {
const backupData = data['activities'];
backupData.forEach(activity => {
const alert = new Alert();
//Problem here need to extract response first
let response = this.abc.getAccounts(activity['accountNum']);
this.longRunningAlerts.push(alert);
this.backupAlerts.push(alert);
});
//this should execute after above is done.
this.totalRecentStatus = backupData.length;
this.longRunningAlerts = [...this.longRunningAlerts];
this.backupAlerts = [...this.backupAlerts];
}
在这里演示:https://stackblitz.com/edit/angular-crxo3g
Observable 在控制台中返回。
【问题讨论】:
-
从一个可观察值转到下一个可观察值时,您需要使用 switchMap。 learnrxjs.io/operators/transformation/switchmap.html
-
您能详细说明一下吗?
-
你能看看@Reactgular stackblitz.com/edit/angular-crxo3g