将 concatMap 结果传递给 rxjs 中的下一个 concatMap [英] passing concatMap result in to next concatMap in rxjs

查看:50
本文介绍了将 concatMap 结果传递给 rxjs 中的下一个 concatMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以看到我有一个方法,它按我的预期工作正常

You can see I have a method , which is working fine as I expected

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

in the 4 bullet point I passing an empty parameter to the createTemplate method , actually I have to pass basedntheTemplateresponse(in code rootTemplate). Right now I am doing a tweak by setting up when execute getAllChildTemplates

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

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

is there any right way to pass the rootTemplate from the pipe itself to the next concatMap ? I am not sure is this is the right approach.

推荐答案

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

You can just map the result from the inner Observable into an array of the current response and the previous one:

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

这篇关于将 concatMap 结果传递给 rxjs 中的下一个 concatMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆