我有一個嵌套的forkjoin的問題,當我嘗試訂閱拉,它回來我的數據,但我可以看到,getitem3仍在運行。如何要等到所有的功能都完成之前,訂閱如何等待嵌套forkjoin完成?
見例如
pull(id) {
return Observable.forkJoin(
this.getitem1(id),
this.getitem2(id),
this.getitem3(id)
);
}
getitem3(id) {
let url = 'https://host3/get/' + id;
let observableBatch = [];
return this.http.get(url).map(res => res.json()).map(data => {
data.posts.data.forEach((item) => {
observableBatch.push(this.getImageUrl(item.id));
})
return Observable.forkJoin(observableBatch);
})
}
getitem2(id) {
let url = 'https://host2/get/' + id;
let observableBatch = [];
return this.http.get(url).map(res => res.json()).map(data => {
data.posts.data.forEach((item) => {
observableBatch.push(this.getImageUrl(item.id));
});
return Observable.forkJoin(observableBatch);
});
}
getitem1(id) {
let url = 'https://host1/get/' + id;
let observableBatch = [];
return this.http.get(url).map(res => res.json()).map(data => {
data.posts.data.forEach((item) => {
observableBatch.push(this.getImageUrl(item.id));
});
return Observable.forkJoin(observableBatch);
});
}
getImageUrl(id) {
let url = 'https://localhost/image/' + id;
return this.http.get(url).map(res => res.json()).map(data => {
console.log('done');
});
}
在'getImageUrl(id)'方法中,您使用'subscribe',因此它調用了url。刪除'subscribe'(所以它會返回一個可觀察的)並重試。 – echonax
我更改了訂閱地圖。我必須在我的getItem中訂閱它嗎? – kabus
不,你應該訂閱'拉(id)'方法,如果這是你唯一需要的。 – echonax