[英]how to receive data from real time database in firebase in angular
Try this to get the messages list from the firestore.试试这个从firestore获取消息列表。 Import AngularFireDatabase service and AngularFireList to store message collection.
导入AngularFireDatabase服务和AngularFireList来存储消息集合。
import { Injectable } from '@angular/core'; import { AngularFireDatabase, AngularFireList } from '@angular/fire/database'; @Injectable({ providedIn: 'root' }) export class MessageService { messages: AngularFireList<any>; constructor(private http: HttpClient, private db: AngularFireDatabase) { } /** * Get All tickers from firebase */ getMessages(): Observable<any> { this.messages = this.db.list('messages'); return this.messages.valueChanges(); } }
Make sure you have imported AngularFireModule and AngularFireDatabaseModule in your app module.确保您已在应用模块中导入AngularFireModule和AngularFireDatabaseModule 。
AngularFire is build with the Observable Pattern via RxJs Library. AngularFire 是通过 RxJs 库使用可观察模式构建的。
You can stream the data with the valueChanges() method, as described in the documentation您可以使用 valueChanges() 方法 stream 数据,如文档中所述
items: Observable<any[]>;
constructor(firestore: AngularFirestore) {
this.items = firestore.collection('items').valueChanges();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.