Angular & RxJS5 - 保留来自内部 Observable 的值超出高阶 Observable

Angular & RxJS5 - Retain values from inner Observable beyond a higher order Observable

我从 Angular 的 route.params 的内部 Observable 开始。

我希望能够保留参数 'beyond' 高阶可观察值 switchMap 的值,在我的例子中它执行 http 调用。

例如:

this.data$ = this.route.params
    .switchMap(params => this.http.get('/api', { param1: params.param1, param2: params.param2 }))
    .map(([params, data]) => {
        //possible to get params AND data here? This returns undefined.
        return something
    })

是否可以访问switchMap之后的参数?

您可以使用 forkJoin:

this.data$ = this.route.params
    .switchMap(params =>  
         Observable.forkJoin([
             Observable.of(params),
             this.http.get('/api', { param1: params.param1, param2: params.param2 })
    ])
    .map(([params, data]) => {

         return something
    })

使用 switchMap 的 ResultSelector 参数

A function to produce the value on the output Observable based on the values and the indices of the source (outer) emission and the inner Observable emission. The arguments passed to this function are

http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-switchMap

this.data$ = this.route.params
  .switchMap(params => this.http.get('/api', { param1: params.param1, param2: params.param2 }), 
    (params, data) => {
       return [params, data];
     })
    .map(([params, data]) => {
      //possible to get params AND data here? This returns undefined.
       return something
     })