使用Observable.combineLatest
或Observable.forkJoin
,看到它们的差异here。
Observable.combineLatest(this.test3, this.test4);
Observable.forkJoin(this.test3, this.test4);
当您订阅Observable.combineLatest
,你会得到结果如下图所示:
[response3, response4]
,您可以使用Array.reduce
到这两个观测量的结果结合起来
// combineLatest
Observable.combineLatest(this.test3, this.test4)
.subscribe(res => {
this.concatResult = res.reduce((pre, next) => {
return [].concat(pre.json().data, next.json().data);
})
});
// forkJoin
Observable.forkJoin(this.test3, this.test4)
.subscribe(res => {
this.concatResult = res.reduce((pre, next) => {
return [].concat(pre.json().data, next.json().data);
})
});
请参阅本plunker demo
我认为'forkJoin'可能更适合http操作。因为'combineLatest'不等待观测数据完成(泄漏可能?)AFAIK,但不确定它是否会产生影响:-) – echonax
@echonax我不熟悉'forJoin',让我先了解它。 :P – Pengyy
检查:https://stackoverflow.com/questions/41797439/rxjs-observable-combinelatest-vs-observable-forkjoin – echonax