角度http请求调用多次

时间:2018-07-11 10:38:21

标签: javascript angular rxjs

我在onInit中有以下代码:

const urlParams = Observable.combineLatest(
  this.route.params,
  this.route.queryParams,
  (params, queryParams) => ({ ...params, ...queryParams })
);

this.id$ = urlParams.map(param => {
  return param.id;
});


this.ofert$ = this.id$.pipe(
  filter(id => !!id),
  switchMap((ofertId: number) => {
    return this.offerService.getWorkOffer(ofertId);
  })
);

为什么this.offerService.getWorkOffer中的switchMap打电话了 多次?

1 个答案:

答案 0 :(得分:1)

this.id$ = urlParams.map.... <= this.id$是与urlParams相同大小的集合/数组。 pipe调用将迭代每个成员,然后依次调用switchMap,然后在每次迭代中调用this.offerService.getWorkOffer