我正在尝试在angular 4中执行顺序的http请求。我有一个http请求,它返回一个可观察的对象,如下所示:
repos$: Observable<Repo[]>;
this.repos$ = this.candidateService.getCandidateList(); // this method performs the http request
getCandidateList(){
this.http.get<Repo[]>('https://api.github.com/user/repos', {headers});
}每个返回的Repo对象都包含一个名为subscribers_url的字段,我需要提取该字段并对其执行GET请求。有没有办法做到这一点?我试过使用mergeMap,但它似乎不起作用。
编辑:
interface Repo {
name: string;
url: string;
full_name: string;
subscribers_url: string;
}发布于 2017-09-20 10:30:52
我认为您可以遍历返回的repos列表,然后调用另一个调用,传入每个repo的url,如下所示:
repos$.subscribe({
repo => {
for(var i = 0; i < repo.length; i++){
this.dynamicGetRequest(repo[i].url).subscribe({
res=>console.log(res);
})
}
}
});
dynamicGetRequest(url) {
this.http.get<any>(url, {headers});
}发布于 2017-09-20 10:48:57
试试这个
repos$: Observable<Repo[]>;
this.repos$ = this.candidateService.getCandidateList()
.switchMap((list: Repo[]) => {
let requests = list.map((i) => {
return this.secondRequest(i)
})
return Observalbe.forkJoin(requests).map((dataList)=>{
return dataList.map((data, index) => {
return {
...list[index],
collaborator: data
}
})
})
})
getCandidateList(){
this.http.get<Repo[]>('https://api.github.com/user/repos', {headers});
}https://stackoverflow.com/questions/46312222
复制相似问题