2017-05-25 43 views

回答

3

使用Observable.combineLatestObservable.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

+0

我認爲'forkJoin'可能更適合http操作。因爲'combineLatest'不等待觀測數據完成(泄漏可能?)AFAIK,但不確定它是否會產生影響:-) – echonax

+0

@echonax我不熟悉'forJoin',讓我先了解它。 :P – Pengyy

+0

檢查:https://stackoverflow.com/questions/41797439/rxjs-observable-combinelatest-vs-observable-forkjoin – echonax

相關問題