简体   繁体   English

带有内部地图订阅的Angular2 rxjs switchMap发出多个可观察对象

[英]Angular2 rxjs switchMap with inner map subscribtion emitting multiple observables

I have a switch map with an inner map function which is returning multiple obersables like so: 我有一个带有内部地图功能的开关地图,该地图返回了多个可观察对象,如下所示:

  this.fb
    .getUsersGroupsAsObservable(user.uid, 'contacts')
    .switchMap(groups => {
      return groups.map(group => this.fb.getJobsbyGroup(group.id));
    })
    .subscribe(res => {
      console.log(res); // emits Observable {_isScalar: false, source: Observable, operator: MapOperator}
      this.job$ = res;
    });
});

This is running twice correctly and returning two observables like so: 这将正确运行两次,并返回两个可观察值,如下所示:

Observable {_isScalar: false, source: Observable, operator: MapOperator}
Observable {_isScalar: false, source: Observable, operator: MapOperator}

Is there any way to return a list of observables (thinking AngularFireList) which I can then render with an async pipe? 有没有什么方法可以返回可观察对象的列表(认为是AngularFireList),然后可以使用异步管道进行渲染?

Update: @concat helped me to get an answer to this (but for reason the answer was deleted), I updated my code as follows (its now working perfectly): 更新:@concat帮助我得到了一个答案(但是由于答案被删除的原因),我更新了我的代码,如下所示(现在可以正常使用):

  this.fb
    .getUsersGroupsAsObservable(user.uid, 'contacts')
    .switchMap(groups =>
      combineLatest(groups.map(group => this.fb.getJobsbyGroup(group.id))),
    )
    .subscribe(res => {
      console.log(res);
      this.jobs = [].concat.apply([], res);
    });

@concat helped me to solve this problem, all I needed to do was to use combineLatest and that would combine all of my arrays into a single array, I then flattened that array, there might be a simpler way to do this but hey it works: @concat帮助我解决了这个问题,我所要做的就是使用CombineLatest,它将所有数组组合到一个数组中,然后我将其展平,可能会有更简单的方法来做到这一点,但是嘿,它可行:

  this.fb
    .getUsersGroupsAsObservable(user.uid, 'contacts')
    .switchMap(groups =>
      combineLatest(groups.map(group => this.fb.getJobsbyGroup(group.id))),
    )
    .subscribe(res => {
      console.log(res);
      this.jobs = [].concat.apply([], res);
    });

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

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