Angular 角度:从服务可观察值返回

Angular 角度:从服务可观察值返回,angular,rxjs,Angular,Rxjs,我有一个组件,在其中我从服务调用函数: // Component constructor (myService: MyService) { } getData() { this.myService.getTableData() } 如何在组件中的getData方法内接收finalize运算符内接收的id?我想要一些类似于: getData() { this.myService.getTableData().then(id => console.log(id)) } 从组件

我有一个组件,在其中我从服务调用函数:

// Component
constructor (myService: MyService) { } 

getData() {
  this.myService.getTableData()
}
如何在组件中的getData方法内接收finalize运算符内接收的id?我想要一些类似于:

getData() {
   this.myService.getTableData().then(id => console.log(id))
}

从组件而不是服务订阅。此外,您不必将then用于可观察。只使用订阅。然后是用来处理承诺

// Service
getTableData() {
   return task.snapshotChanges().pipe(
     finalize(() => ref.getTableId())
   )
}

getData() {
   this.myService.getTableData().subscribe(id => console.log(id))
}

然后意味着您必须从该方法返回承诺,您需要订阅以从Observable获取值为什么在这种情况下使用finalize?什么是id=>id?看起来你在寻找concatMap或mergeMap,但你想做什么还不清楚。finalize块到底是用来做什么的?@martin我使用finalize是因为firebase文档说要将它用于所需的功能。
// Service
getTableData() {
   return task.snapshotChanges().pipe(
     finalize(() => ref.getTableId())
   )
}

getData() {
   this.myService.getTableData().subscribe(id => console.log(id))
}