简体   繁体   English

角rxjs。 结合多个可观察物

[英]Angular rxjs. combine multiple observables

I have 2 observables: 我有2个观察值:

  1. this.configsService.getConfigsList()
  2. this.bcProductService.getProductById(config['id'])

I can subscribe to both of them and use their data. 我可以订阅他们两个并使用他们的数据。

I want to get the configs list and map each config item with its corresponding product (using the config.id)... Something like this. 我想获取配置列表,并将每个配置项与其对应的产品(使用config.id)进行映射...类似这样。

    const configs = this.configsService.getConfigsList;
    const product= (id)=> this.bcProductService.getProductById(id);
    configs.pipe(
        concatMap(val => product(val['id']).map(t=>val['p']=t) )
    ).subscribe(test => {
        console.log('configs with products: ', test);
    });

but that is not working. 但这不起作用。 the above does not print an error or the console.log. 以上不会显示错误或console.log。 I see several examples of this online but i just can seem to get it working. 我在网上看到了几个例子,但我似乎可以使它正常工作。 Thank you for helping. 感谢您的帮助。

another attempt 另一个尝试

this is the example from https://www.learnrxjs.io/operators/combination/combinelatest.html but i get the following error. 这是来自https://www.learnrxjs.io/operators/combination/combinelatest.html的示例,但出现以下错误。

core.js:1673 ERROR TypeError: combinedProject.subscribe is not a function core.js:1673错误TypeError:combinedProject.subscribe不是函数

configsWithP(): any {
    const configs = this.configsService.getConfigsList;
    const product = this.bcProductService.getProductById(124);
    const combinedProject = combineLatest( configs, product, (c, p) => {
        return `c: ${c}, p: ${p}`;
    });
    // log values
    const subscribe = combinedProject.subscribe(latestValuesProject =>
        console.log(latestValuesProject)
    );
}

Updated answer after we discussed it in chat and made a solution. 在聊天中讨论了答案并提出解决方案后,更新了答案。 The getConfigLists returns an Observable that emits arrays of config objects. getConfigLists返回一个Observable,它发出配置对象数组。 Then for every object in the array the corresponding product needs to be retrieved from a service and merged with the config object. 然后,对于数组中的每个对象,需要从服务中检索相应的产品,然后将其与config对象合并。 After that it needs to again come out as an observable that emits the array of config object. 之后,它需要再次作为可观察到的对象发出配置对象数组。

This is the solution: 这是解决方案:

private configsWithP(): any {
  const configs = this.configsService.getConfigsList;
  const combinedConfigs = configs.pipe(
    flatMap((configsArray) => {
      const getProductObservablesArray = configsArray.map((config) => this.bcProductService.getProductById(124));
      return zip(...getProductObservablesArray, (...args: any[]) => args).pipe(
        map(products => {
          for (let i = 0; i < configsArray.length; i++) {
            configsArray[i]['p'] = products[i];
          }
          return configsArray;
        })
      )
  }));
  combinedConfigs.subscribe(configss=> {
    console.log(configss);
  });
}

Here is also a sample on StackBlitz that works. 这也是适用于StackBlitz的示例。 I did not create services I create observables and combine them. 我没有创建服务,而是创建了可观察对象并将它们组合在一起。 Check the console there and you will see the output. 检查那里的控制台,您将看到输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM