【问题标题】:passing concatMap result in to next concatMap in rxjs将 concatMap 结果传递给 rxjs 中的下一个 concatMap
【发布时间】:2018-10-24 00:21:37
【问题描述】:

你可以看到我有一个方法,它按我的预期工作得很好

loadTemplates(configUrl: any, templateId: string): Observable<any> {
    this.getConfiguration(configUrl)
      .pipe(

        concatMap(configResponse =>
          this.getTemplate(
            CONFIGURATION_URL +
              "templates/" +
              configResponse.TemplateSettings.RootTemplate.Path,
            configResponse
          )
        ),
        concatMap(rootTemplate =>
          this.templateEngine.getAllChildTemplates(rootTemplate)       
        ),
        concatMap(childTemplates=>this.templateEngine.createTemplateToRender(childTemplates,""))
      )
      .subscribe(finalresponse=> {

        debugger;
      });

  }
}

执行

  1. getTheConfiguration() => 基于配置响应=>
  2. 呼叫 getTemplate(basedontheconfigresponse)=>basedntheTemplateresponse=>
  3. 调用 getAllChildTemplates(basedntheTemplateresponse)=> 基于子模板响应=>
  4. 调用 createTemplate(basedonthechildtemplateresponse,"")

在第 4 个要点中,我将一个空参数传递给 createTemplate 方法,实际上我必须传递 basedntheTemplateresponse(在代码 rootTemplate 中) .现在我正在通过设置执行时进行调整 getAllChildTemplates

getAllChildTemplates(parentTemplate: string) {
    this.currentrootTemplate = parentTemplate;
    var services = this.getChildTemplateServiceInstance(parentTemplate);
    return forkJoin(services);
  }

是否有任何正确的方法将 rootTemplate 从管道本身传递到下一个 concatMap ?我不确定这是不是正确的方法。

【问题讨论】:

    标签: angular rxjs


    【解决方案1】:

    您可以将内部 Observable 的结果映射到当前响应和前一个响应的数组中:

    concatMap(rootTemplate =>
      this.templateEngine.getAllChildTemplates(rootTemplate).pipe(
        map(childTemplates => [rootTemplate, childTemplates])
      )
    ),
    concatMap(([rootTemplate, childTemplates]) => 
      this.templateEngine.createTemplateToRender(childTemplates, rootTemplate)
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多