Firestore valueChanges() 仅在使用 take(1) 时发出一次

Firestore valueChanges() only emits once when using take(1)

我的 firestore 数据库的 counts 集合 (counts/booking) 中有一个名为 booking 的文档。我想从这个预订文件中读取数据并监听值的变化。我如何使用 AngularFire2 实现此目的?

我试过下面的代码,

this.afs.collection('counts').doc<Count>('booking').valueChanges().pipe(take(1))
      .subscribe(data => {
        if (data) {
          this.newCount = data.newCount;
          this.otherCount = data.otherCount;
        }
      });

这会读取值,但似乎不会在 firestore 文档中更改时更新值。

删除 take(1) 管道运算符,因为这将导致您的代码有效地仅 execute/emit 单 (1) 次:

this.afs.collection('counts').doc<Count>('booking').valueChanges()
  .subscribe(data => {
    if (data) {
      this.newCount = data.newCount;
      this.otherCount = data.otherCount;
    }
  });

希望对您有所帮助!