Angular4:HttpClient-连续许多请求 [英] Angular4: HttpClient - many requests in succession
本文介绍了Angular4:HttpClient-连续许多请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个http get请求,以从提要中获取某些项目的列表. feed API需要一个页面参数,该参数限制为20.我想获取前60个参数,因此我需要发出3个请求
I have a http get request to get a list of some items from a feed. the feed API requires a page param, which is limited to 20. I would like to get first 60 instead, so I need to make 3 requests
requestOptions.params.set('page', 1);
this.http.get(environment.api+ '.feed.json', requestOptions)
requestOptions.params.set('page', 2);
this.http.get(environment.api+ '.feed.json', requestOptions)
requestOptions.params.set('page', 3);
this.http.get(environment.api+ '.feed.json', requestOptions)
处理它以获得有序列表的最佳方法是什么?我不太喜欢嵌套
what is the best way to deal with it to get an ordered list? I am not very fond of nesting
推荐答案
您的服务
getPage(page) {
/*init requestOptions*/
requestOptions.params.set('page', page);
return this.http
.get(environment.api+ '.feed.json', requestOptions)
.map(/*map your object to the expected one*/)
.catch(/*catch any http exception*/);
}
您在哪里致电服务
let allPages = [];
Observable.forkJoin(
this.service.getPage(1);
this.service.getPage(2);
this.service.getPage(3);
).subscribe((respArray) =>{
allPages = allPages.concat(respArray[0]);
allPages = allPages.concat(respArray[1]);
allPages = allPages.concat(respArray[2]);
});
Object.forkJoin
将等待所有请求完成,然后您可以使用此结果构建一个列表;
Object.forkJoin
will wait until all requests are completed and then you can build a single list with this results;
这篇关于Angular4:HttpClient-连续许多请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文