如何订阅 AngularFire 文档

How to subscribe to an AngularFire document

我有一个组件可以查询服务中的 angularfire 文档。下面是有效的,因为我从我正在使用的另一个需要 activatedRoute 检查的函数复制并粘贴了它。在这种情况下,我不需要激活的路由检查。

但是,我不确定删除该特定检查的语法,但查询数据库的结果仍然相同,在 return 中获取结果并订阅它。

Component.ts

  testQuery(id: string) {
    this._subscription = this._activatedRoute.params.pipe(
      switchMap(params => {
        return this.service.getInfo(id);
      })
    ).subscribe(details => {
      // Subscription stuff and form patchValue and behaviorSubject updates
    })
  }

Service.ts

getInfo(id: string): Observable<any[]> {
  return this.afs.doc<any>(`collection/${id}`).valueChanges().pipe(shareReplay());
}

只需删除那段代码和不再需要的 pipe(switchMap...

testQuery(id: string) {
  this._subscription = this.service.getInfo(id).subscribe(details => {
    // Subscription stuff and form patchValue and behaviorSubject updates
  });
}