Angular 使用forkjoin合并http观察值

Angular 使用forkjoin合并http观察值,angular,typescript,ionic2,observable,Angular,Typescript,Ionic2,Observable,我试图通过使用forkjoin来避免嵌套的观测值。当前(嵌套)版本如下所示: this.http.get('https://testdb1.firebaseio.com/.json').map(res => res.json()).subscribe(data_changes => { this.http.get('https://testdb2.firebaseio.com/.json').map(res => res.json()).subscribe(data

我试图通过使用
forkjoin
来避免嵌套的观测值。当前(嵌套)版本如下所示:

  this.http.get('https://testdb1.firebaseio.com/.json').map(res => res.json()).subscribe(data_changes => {
    this.http.get('https://testdb2.firebaseio.com/.json').map(res => res.json()).subscribe(data_all => {
      /* Do this once resolved */
      this.platform.ready().then(() => {
        this.storage.set('data_changes', data_changes);
        this.storage.set('data_all', data_all);
        document.getElementById("chart").innerHTML = "";
        this.createChart();
      });
    });
  },

    err => {
      this.platform.ready().then(() => {
        console.log("server error 2");
        document.getElementById("chart").innerHTML = "";
        this.createChart();
      });
    });
  }
我可以将第一部分改写为:

Observable.forkJoin(
  this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()),
  this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json())
)
但我不确定接下来如何添加
.subscribe
方法来访问
数据更改
数据所有


看看另一个例子,我知道它应该看起来像
.subscribe(res=>this.combined={friends:res[0]。friends,customer:res[1]}),但我不确定如何使其适应我的示例

尝试使用
combinelatetest
而不是
forkJoin

组合测试

const combined = Observable.combineLatest(
  this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()),
  this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json())
)

combined.subscribe(latestValues => {
    const [ data_changes , data_all ] = latestValues;
    console.log( "data_changes" , data_changes);
    console.log( "data_all" , data_all);
});
const combined = Observable.forkJoin(
  this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()),
  this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json())
)

combined.subscribe(latestValues => {
    const [ data_changes , data_all ] = latestValues;
    console.log( "data_changes" , data_changes);
    console.log( "data_all" , data_all);
});
您也可以通过forkJoin来处理它,但是forkJoin将在所有调用完成时返回数据并返回结果,但是在combinetest中,当任何可观察对象发出值时,将从每个对象发出最新的值

使用
forkJoin

const combined = Observable.combineLatest(
  this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()),
  this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json())
)

combined.subscribe(latestValues => {
    const [ data_changes , data_all ] = latestValues;
    console.log( "data_changes" , data_changes);
    console.log( "data_all" , data_all);
});
const combined = Observable.forkJoin(
  this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()),
  this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json())
)

combined.subscribe(latestValues => {
    const [ data_changes , data_all ] = latestValues;
    console.log( "data_changes" , data_changes);
    console.log( "data_all" , data_all);
});

打电话给这两位并查看控制台日志,你会明白的。

如果其中包括解释OP为什么应该支持
CombineTest
,这将是一个更好的答案。好的,请等一分钟。如果你发现这一有用信息,请也接受答案。你对
CombineTest
行为的描述不正确。它不会发射任何东西,直到它经过的每个观察物至少发射一次。HTTP observables只会发出一次,那么使用它与使用
forkJoin
有何不同呢?请注意,
forkJoin
仅在调用
forkJoin
后,当observables返回新值时才会触发,因此,如果其中一个观察值已经返回了一个值,
forkJoin
将永远不会触发下一个
CombineTest
将触发,即使可观察对象已发出其值。我想这可能会帮助一些人,如果他们看到这个页面,却不明白为什么他们的订阅下一个功能永远不会被使用。