Angular 等待多个 http 请求完成,然后触发最后一个 [英] Angular wait for multiple http requests to complete and then fire the last one
本文介绍了Angular 等待多个 http 请求完成,然后触发最后一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 4 个 http 请求.第三个接收查找值,而第四个获取实际表单数据.
I have 4 http requests. Three receives lookup values while the 4th one gets actual form data.
他们是这样的:
let medicalData = this.data.getCodes('medical').subscribe((data: {}) => {
console.log('med');
this.Medical = data;
});
let delayData = this.data.getCodes('delay').subscribe((data: {}) => {
console.log('del');
this.Delays = data;
});
let disabilityData = this.data.getCodes('disability').subscribe((data: {}) => {
console.log('dis');
this.Disability = data;
});
let districtData = this.data.getCodes('district').subscribe((data: {}) => {
console.log('dist');
this.District = data;
});
如何让第 4 个 get 请求等到前三个请求完成?
How can I make the 4th get request wait till the first three requests are complete ?
提前致谢
推荐答案
你应该使用 forkJoin
来达到预期的结果.forkJoin
在发出值之前等待所有可观察对象完成.示例:
You should use forkJoin
to achieve the desired result. forkJoin
waits for all obesrvables to complete before emitting a value. Example:
forkJoin(
this.data.getCodes('medical'),
this.data.getCodes('delay'),
this.data.getCodes('disability'),
this.data.getCodes('district'),
).subscribe(([medicalData, delayData, disabilityData, districtData]) => {
this.Medical = medicalData;
this.Delays = delayData;
this.Disability = disabilityData;
this.District = districtData;
// make your last http request here.
});
这篇关于Angular 等待多个 http 请求完成,然后触发最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文