根据页面数量动态合并httpClient Observable

时间:2018-05-31 10:59:11

标签: angular rxjs angular-httpclient

我有API,它通过页面(页面)提供数据。我在第一次请求时获得了页数。

在客户端上,我需要在一个Observable中连接页面。尝试这样的事情:

all$: Observable<Service[]>;
iterator$ = new BehaviorSubject(1);

constructor(public http: HttpClient) {
  this.all$ = this.iterator$.pipe(
    concatMap(page => http.get<Service[]>('/service', {params: {page: page.toString()}}).pipe(
      tap(() => (ifNotLastPage) ? this.iterator$.next(++page) : null),
    )),
  );
}

并且只获得最后一页

1 个答案:

答案 0 :(得分:0)

Finnaly我得到了:

all$: Observable<Service[]>;

constructor(public http: HttpClient) {
  this.all$ = this.getData().pipe(
    expand(result => {
      const next = getNextPageNumber(result);

      return (next) ? this.getData(next) : empty();
    }),
    map(result => result.body),
    concatMap(typeCasting(Service)),
    toArray<Service>(),
  );

}

getData(page = 1): Observable<HttpResponse<Config>> {
  return this.http.get<Config>('/service', { observe: 'response', params: { page: page.toString() } });
}

Thanx到https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html 和这篇文章@martin