亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號