繁体   English   中英

角度:服务可观察值的回报

[英]Angular: return from service observable value

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

// Component
constructor (myService: MyService) { } 

getData() {
  this.myService.getTableData()
}
// Service
getTableData() {
   task.snapshotChanges().pipe(
     finalize(() => {
       ref.getTableId().subscribe(id => id))
     })
   ).subscribe()
}

我如何在组件的getData()方法内的finalize运算符内收到此id 我想要类似的东西:

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

从组件而不是服务订阅。 另外,您不必then用于观察。 仅使用subscribe then用来履行诺言。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM