给定一个数组:
// hero.service.ts
hero: Observable<Hero[]>;
你会如何做类似的事情:hero.push(newHero)?
最佳答案
不确定您想要实现什么目标,但自从您标记了 firestore 以来。我想你可以使用 angularfirestorecollection 并稍后将它们映射到可观察数组。
cardsCollection: AngularFirestoreCollection<Card>;
cardDoc: AngularFirestoreDocument<Card>;
cards: Observable<Card[]>;
在初始化时,您会将它们绑定(bind)在一起。
ngOnInit() {
this.cards = this.cardsCollection.snapshotChanges().map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as Card;
data.id = a.payload.doc.id;
return data;
});
});
}
然后您可以将卡片添加到 AngularFirestoreCollection。
addCard(card: Card) {
this.cardsCollection.add(card);
}
希望这有帮助。如果您想查看我的完整代码和项目,请访问我的 github .
关于ANGULAR 如何推送到可观察数组?英雄 : Observable<Hero[]>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50181350/