为了账号安全,请及时绑定邮箱和手机立即绑定

Angular 9:forkJoin 订阅不工作

Angular 9:forkJoin 订阅不工作

慕盖茨4494581 2023-02-17 11:05:16
我正在尝试使用 observables (rxjs 6.5.1) 在 Angular 9 的顶级组件上加载页面数据。当我单独订阅这些服务中的每一项时,我可以看到返回的数据很好: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));}当我尝试使用 forkJoin 时,从未返回订阅方法中的数据: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)  );}我试过传递 forkJoin 一系列服务调用,我也试过使用 zip,但无济于事。这里发生了什么事?
查看完整描述

1 回答

?
月关宝盒

TA贡献1772条经验 获得超5个赞

我建议使用combineLatestfrom rxjs。它更易于使用


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));

}


查看完整回答
反对 回复 2023-02-17
  • 1 回答
  • 0 关注
  • 119 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信