簡體   English   中英

如何從 angular 中的 firebase 中的實時數據庫接收數據

[英]how to receive data from real time database in firebase in angular

我想從 firebase 的實時數據庫中接收數據。 數據如下圖: 在此處輸入圖像描述

我試過的代碼是

return this.db.list('messages', ref => {
  return ref.limitToLast(25).orderByKey();
});

但它不工作

試試這個從firestore獲取消息列表。 導入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(); } }

確保您已在應用模塊中導入AngularFireModuleAngularFireDatabaseModule

AngularFire 是通過 RxJs 庫使用可觀察模式構建的。

您可以使用 valueChanges() 方法 stream 數據,如文檔中所述

items: Observable<any[]>;
constructor(firestore: AngularFirestore) {
   this.items = firestore.collection('items').valueChanges();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM