【问题标题】:Merging http observables using forkjoin使用 forkjoin 合并 http observables
【发布时间】:2017-08-27 05:34:59
【问题描述】:

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

  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 方法来访问data_changesdata_all

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

【问题讨论】:

    标签: angular typescript ionic2 observable


    【解决方案1】:

    尝试使用combineLatest 而不是forkJoin

    combineLatest

    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);
    });
    

    你也可以通过 forkJoin 来处理,但是 forkJoin 会在所有调用完成后返回数据并返回结果,但是在 combineLatest 中任何一个 observable 发出一个值时,都会从每个中发出最新的值。

    forkJoin

    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 应该支持combineLatest
    • 好的,一分钟。
    • 如果你觉得这个有帮助,也请采纳。
    • 您对combineLatest行为的描述不正确。在它传递的每个可观察对象至少发出一次之前,它不会发出任何东西。 HTTP observables 只会发出一次,那么使用它与使用forkJoin 有何不同?
    • 附注,forkJoin 只会在您调用 forkJoin 后可观察对象返回新值时触发,因此如果其中一个可观察对象已经返回值 forkJoin 将永远不会触发下一个.即使 observable 已经发出了它的值,combineLatest 也会触发。认为如果遇到此页面并且无法弄清楚为什么他们的订阅下一个功能永远不会成为它的人,这可能会有所帮助。
    猜你喜欢
    • 2019-02-17
    • 2018-03-13
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    相关资源
    最近更新 更多