You can use forkJoin
for this, Dont forget to include(您可以为此使用forkJoin
,不要忘记添加)
import { forkJoin } from 'rxjs';
forkJoin waits for each HTTP request to complete and group's all the observables returned by each HTTP call into a single observable array and finally return that observable array.(forkJoin等待每个HTTP请求完成并将每个HTTP调用返回的所有可观察对象分组到单个可观察数组中,最后返回该可观察数组。)
getPlanet(pageIndex) {
return this.http.get < Planets > (`${this.url}${pageIndex}`, {
headers: this.headers
});
}
getAllPlanets() {
const response = [...Array(7).keys()].map(i => this.getPlanet(i));
return forkJoin(response);
}
in your component you can call getAllPlanets
(在您的组件中,您可以调用getAllPlanets
)
this.getPlanetsService.getAllPlanets()
.subscribe(res => {
console.log(res);
}, err => {
console.log(err);
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…