【发布时间】:2020-01-29 04:23:04
【问题描述】:
当我需要在 Angular 组件中访问数据时,从可观察流中访问数据的最佳方式是什么?我知道我可以在模板中使用异步管道,但我需要访问实际组件中的数据。
到目前为止,我已经通过点击或订阅将数据提取到一个新变量中,但我只是好奇我是否错过了一种更简单或更简洁的方法。如果没有,在点击和订阅之间是否有首选方法?
//assume db.getBook() returns an observable with a book object
this.subscription = db.getBook("153").pipe(
tap(book => this.book = book).subscribe()
//or
this.subscription = db.getBook("153").subscribe(book => this.book = book)
【问题讨论】: