Angular2从包装的Html.get

时间:2017-10-25 15:18:16

标签: angular rxjs observable

我需要等待这个observable返回(简化):

assembleOptions(options?: RequestOptionsArgs): Observable<boolean> {

    let auth = this._cookieService.get(this.auth);

    if (auth === undefined || auth === null) {
        this._http.get('/someValue').map(x => {
            //do something with x
            return Observable.of(true);
        })
    } else {
        return Observable.of(true);
    }
}

这似乎返回Observable<true>并不重要,因为我没有使用它,我只需要等待它,然后我尝试在其中使用它:

public get(url: string, options?: RequestOptionsArgs): Observable<Response> {
  return this.assembleOptions(options).map(x => {
      return this._http.get(url, this.options);
  });
}

this._http来自

  

从&#39; @ angular / http&#39;;

导入{Http,Response,Headers,RequestOptionsArgs}

哪个不起作用,它返回Observable<Observable<Response>>

Type 'Observable<Observable<Response>> is not assignable to type 'Observable<Response>'

我在这里缺少什么?我试过无处不在的地方无所事事......谢谢!

1 个答案:

答案 0 :(得分:2)

您希望switchMap代替map

return this.assembleOptions(options).switchMap(x => {
      return this._http.get(url, this.options);
  });

来自switchMap documentation

  

映射到可观察,完整的先前内部可观察,发射值