【发布时间】:2021-08-03 11:10:11
【问题描述】:
我只想在我的 Firebase 文档数据更改时调用一次函数,但是将 .subscribe() 链接到我的 valueChanges() 会导致订阅每次有更新时触发两次。
无需订阅的基本代码:
home.component.ts:
constructor(private firestore: AngularFirestore) { }
ngOnInit(): void {
let docName = this.buildDocName();
this.plants = this.firestore.collection('officePlants').doc(docName).valueChanges();
}
home.component.html:
<mat-list *ngIf="(plants | async)">
<mat-list-item>Humidity: {{ (plants | async)?.humidity }}</mat-list-item>
<mat-list-item>Temperature: {{ (plants | async)?.temperature }}</mat-list-item>
</mat-list>
此代码导致订阅触发两次:
home.component.ts:
ngOnInit(): void {
let docName = this.buildDocName();
this.plants = this.firestore.collection('officePlants').doc(docName).valueChanges()
.subscribe(data => {
if (!data)
return;
console.log('doc updated...', data);
// ...
});
}
【问题讨论】:
标签: angular firebase google-cloud-platform google-cloud-firestore rxjs