簡體   English   中英

將concatMap結果傳遞到rxjs中的下一個concatMap

[英]passing concatMap result in to next concatMap in rxjs

您可以看到我有一個方法,它按預期工作正常

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(基於配置響應)=> basedntheTemplateresponse =>
  3. 調用getAllChildTemplates(basedntheTemplateresponse)=> based onthechildtemplateresponse =>
  4. 調用createTemplate(基於子模板響應,“”)

在第4點,我將一個空參數傳遞給createTemplate方法,實際上我必須傳遞basedntheTemplateresponse (在代碼rootTemplate中 )。 現在我正在通過執行getAllChildTemplates時進行設置進行調整

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

有沒有正確的方法將rootTemplate從管道本身傳遞到下一個concatMap 我不確定這是正確的方法。

您可以將結果從內部Observable映射到當前響應和上一個響應的數組中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM