先决条件:
dishService.query
:返回Observable
(菜肴清单);每道菜都具有lat
,lng
属性
geoLocationService.getDistanceFromLatLng$
:返回Observable
,传递时的距离:lat
,lng
坐标;
代码:
this.pageSubject.pipe(
switchMap((filter: DishFilter)=>this.dishService.query(filter)),//gets a list of dishes
tap(_=>console.log("after switchmap", _)
concatMap(page=>combineLatest(page.results.map(dish=>
this.geoLocationService.getDistanceFromLatLng$({lat: user.current_location.lat, lng: user.current_location.lng}, {lat: dish.restaurant_lat, lng: dish.restaurant_lng}).pipe(
map(distance=>dish.distance = distance),
tap(_=>console.log("after distance", _))
)
)
), (page, results) => {
return page
}
),//for each dish attach a distance property using geoLocationService.getDistanceFromLatLng$;Observable
tap(=>console.log("after concatmap")),
).subscribe((page: Page)=>{
//use page somewhere
console.log("completed")
this.cdRef.detectChanges();
});
我做到了:
this.pageSubject.next(filter);//logs to console: after switchmap, after distance..., after concatmap, completed
this.pageSubject.next(filter);//logs to console: after switchmap
在第二次调用pageSubject.next(filter)
时,concatMap不执行。为什么?