[英]Angular 9: forkJoin subscribe not working
I'm trying to load page data on a top level component in Angular 9 using observables (rxjs 6.5.1).我正在尝试使用 observables (rxjs 6.5.1) 在 Angular 9 的顶级组件上加载页面数据。 When I subscribe to each of these services individually, I can see the data coming back just fine:
当我单独订阅这些服务时,我可以看到数据恢复正常:
ngOnInit(): void {
const technicianSubscription = this.techniciansClientService.getByTechnicianId(this.technicianId).subscribe(technician => console.log(technician));
const technicianReviewsSubscription = this.technicianReviewsClientService.getByTechnicianId(this.technicianId).subscribe(technicianReviews => console.log(technicianReviews));
}
When I try to use forkJoin, the data from the subscribe method is never returned:当我尝试使用 forkJoin 时,永远不会返回来自 subscribe 方法的数据:
ngOnInit(): void {
this.pageDataSubscription = forkJoin({
technician: this.techniciansClientService.getByTechnicianId(this.technicianId),
technicianReviews: this.technicianReviewsClientService.getByTechnicianId(this.technicianId),
}).subscribe(
// data is never logged here
data => console.log(data)
);
}
I've tried passing forkJoin an array of service calls and I've tried using zip as well, to no avail.我试过通过 forkJoin 一系列服务调用,我也试过使用 zip ,但无济于事。 What's happening here?
这里发生了什么事?
I would recommend using combineLatest
from rxjs
.我建议使用
combineLatest
的rxjs
。 Its easier to use它更容易使用
import {combineLatest} from `rxjs`;
componentIsActive = true;
ngOnInit(): void {
combineLatest([
this.techniciansClientService.getByTechnicianId(this.technicianId),
this.technicianReviewsClientService.getByTechnicianId(this.technicianId),
]).pipe(
map(([technician, technicianReviews]) => ({technician, technicianReviews})),
takeWhile(() => this.componentIsActive)
).subscribe(data => console.log(data));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.