简体   繁体   English

角度2的多个(相似)可观察对象的组合

[英]Angular 2 combining of multiple (similar) observables

I've found many examples detailing how multiple observable calls can be combined into a single Observable call, but from what I have found, these calls always assume at least one of the following: 我发现了许多示例,这些示例详细说明了如何将多个可观察的调用合并为一个可观察的调用,但是从我发现的情况来看,这些调用始终假定至少以下之一:

1: the number of calls is known 2: the number of calls is fixed 3: the calls are different objects combined into one. 1:已知通话数量2:固定通话数量3:通话是将不同的对象组合为一个。 (as in arg1 => call1, arg2 => call2) (如arg1 => call1,arg2 => call2)

But I've failed to find any decent information as to how to handle the combination of Observables when these calls are made dynamically or when rather than mapping results to different variables, you wish to expand on a list you're trying to collect. 但是,当这些调用是动态进行的,或者不是将结果映射到不同的变量,而是希望扩展到您要收集的列表时,我没有找到任何有关如何处理Observable组合的体面信息。

To give an use-case in which this could apply: An user can sign up to several "groups" of products. 给出一个适用的用例:用户可以注册多达几个“组”的产品。 Each group is linked with an ID, which is used to fetch the products from a remote service. 每个组均与一个ID链接,该ID用于从远程服务获取产品。 The group is to fetch all products from all groups, join them together, and display them to the user. 该小组将从所有小组中提取所有产品,将它们组合在一起,然后将其显示给用户。

The most logical solution to me seems to be to wait until all calls are completed while gathering the results in another list, and from there it would be easy to compile these lists into a single, wanted result, but I've been unable to find a way to effectively achieve this- And am curious to hear if others might have ideas for how to best tackle this problem. 对我来说,最合乎逻辑的解决方案似乎是等待所有调用完成,然后再将结果收集到另一个列表中,从那里可以很容易地将这些列表编译成单个所需的结果,但是我一直找不到一种有效地实现这一目标的方法-并且很想知道其他人是否对如何最好地解决此问题有想法。

let responses = Array<Observable<Articles>>
    for (let id of subscriptions) {
        let response = this.http.get(producturl + id, headers).map(
            //insert mapping...
        );
    responses.push(response);
}

from here I have tried to use .merge() to try and merge the "responses" together but feel like I'm not familiar enough with the command/ misusing it. 从这里开始,我尝试使用.merge()尝试将“响应”合并到一起,但是感觉好像我对命令不足够熟悉/滥用它。 I've also tried iterating over the Observables and subscribing to them in turn, but this seems both sub-optimal and did not produce the wanted results. 我还尝试了遍历Observable并依次订阅它们,但这似乎既不是最优的,也没有产生想要的结果。 I've tried more, but these two options seem to me to be most likely to be on the right track to a proper solution... If more code is needed to see where the potential issue might lie, I'll gladly share- And thanks for any input. 我已经尝试了更多,但是在我看来,这两个选项最有可能找到适当解决方案的正确途径...如果需要更多代码以查看潜在问题所在,我很乐意分享-并感谢您的任何投入。

You can use forkJoin together with a normal reduce : 您可以使用forkJoin连同正常reduce

let responses: Array<Observable<Articles>> = [];
for (let id of subscriptions) {
    let response: Observable<Articles> = this.http.get(producturl + id, headers).map();
    responses.push(response);
}

Observable.forkJoin(responses)
   .map(response => response.reduce((a,b) => {return a.concat(b)}))
   .subscribe((response: Articles[]) => {
       console.log(response);
      //output is a flat array of all Articles
});

Don't forget to add the rxjs operator: 不要忘记添加rxjs运算符:

import 'rxjs/add/observable/forkJoin';

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

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