【问题标题】:Angular2 Observables and the order of executionAngular2 Observables 和执行顺序
【发布时间】:2016-07-18 20:24:47
【问题描述】:

Observables 是一个流,完成后会执行并完成。很公平。在一个组件中使用服务进行多个 API 调用,是否可以采取任何措施来确保一个可观察对象在另一个可观察对象启动之前完成?

所以在下面的例子中,可以做些什么来确保 getOptions 在 getReport 开始之前完成。 getReport 依赖于 getOptions 获取的数据。

  ngOnChanges() {
    if (this.treeResult != null) {              
      for (var index = 0; index < this.treeResult.length; index++) {        
        var element = this.treeResult[index];               
          this.thumbnailView = null;

          this.getOptions(element.id);            

          this.getReport(element.id, this.a, this.b, element.c);              
      }
    }
  }

  getOptions(id: number) {
    this._Service.GetOptions(id)
      .subscribe(options => { this.select = options[0].Options1, this.num = options[0].Options1[0].optionId,
                              error => this.errorMessage = <any>error)
  }  

  getReport(id: number, a: number, b: number, c: string) {
    this._Service.GetReport(id, a, b, c)
      .subscribe(data => { this.data = data }, error => this.errorMessage = <any>error);
  }

如果需要,很乐意提供更多信息

【问题讨论】:

标签: typescript angular observable


【解决方案1】:

是的。在第一个 observable 的 done() 方法中调用第二个 observable:

 ngOnChanges() {
    if (this.treeResult != null) {              
      for (var index = 0; index < this.treeResult.length; index++) {        
        var element = this.treeResult[index];               
          this.thumbnailView = null;

          this.getOptions(element.id, element.c);                       
      }
    }
  }

  getOptions(id: number, c : any) {
    this._Service.GetOptions(id)
      .subscribe(options => { 
              this.select = options[0].Options1;
              this.num = options[0].Options1[0].optionId;
          }, error => {
              this.errorMessage = <any>error;
          }, () => {
              // done
              this.getReport(id, this.a, this.b, c);
          });
  }  

  getReport(id: number, a: number, b: number, c: string) {
    this._Service.GetReport(id, a, b, c)
      .subscribe(data => { 
               this.data = data;
          }, error => {
              this.errorMessage = <any>error;
          }, () => {
              // done
          });
  }

【讨论】:

    【解决方案2】:

    您可以利用flatMap 运算符来串行执行可观察对象。

    这是在您的情况下使用它的方法:

      ngOnChanges() {
        if (this.treeResult != null) {              
          this.treeResult.forEach(element => {
            this.thumbnailView = null;
    
            this.getOptions(element.id).flatMap(() => { // <------
              this.getReport(element.id, this.a, this.b, element.c);
            }).subscribe();
          });
        }
      }
    
      getOptions(id: number) {
        return this._Service.GetOptions(id)
          .map(options => { // <-----
            this.select = options[0].Options1;
            this.num = options[0].Options1[0].optionId
          })
          .catch(error => this.errorMessage = <any>error);
      }  
    
      getReport(id: number, a: number, b: number, c: string) {
        return this._Service.GetReport(id, a, b, c)
          .map(data => {
            this.data = data;
          })
          .catch(error => this.errorMessage = <any>error);
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-06
      • 2019-10-25
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      相关资源
      最近更新 更多