我有一個 Angular12 應用程式,我使用 AngularFire 從 Firebase Realtime DB 獲取資料。因為我需要在我的代碼中的不同位置使用我的資料,所以我希望我的 DAO 服務能夠獲取 firebase Observable 串列,并且我對它們進行很少的預處理(將字串日期轉換為 Date 物件)。如果我執行此預處理,那么我將失去訂閱能力,現在我的應用程式無法獲得實時資料的好處。所以我想讓那個預處理回傳一個 Observable 串列,但我不知道怎么做。
我的代碼:
FantaroiDAO服務
private list = this.db.list<Fantaros>('fantaroi')
constructor(private db: AngularFireDatabase){}
public getIpir() {
return this.list.valueChanges();
}
應用組件
ngOnInit(): void {
let fantaroi: Fantaros[] = [];
this.dao.getIpir().subscribe(f => {
fantaroi = f.map(f => {
const soc = f.soc.map(date => new Date(date))
const dishes = f.dishes? f.dishes.map(date => new Date(date)) : []
const off = f.off.map(date => new Date(date))
return new Fantaros(f.name, soc, dishes, off);
})
// other Component related code.
});
}
fantaroi是我要監視更改然后使用它來更新我的視圖的串列。有沒有辦法做到這一點?或者我是否必須在使用該串列的每個位置復制粘貼決議到 Date 物件的代碼?
uj5u.com熱心網友回復:
您應該能夠使用運算子來使用pipe和應用轉換map:
public getIpir() {
return this.list.valueChanges().pipe(
map(fantaroi => fantaroi.map(f => {
const soc = f.soc.map(date => new Date(date))
const dishes = f.dishes? f.dishes.map(date => new Date(date)) : []
const off = f.off.map(date => new Date(date))
return new Fantaros(f.name, soc, dishes, off);
})
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/405869.html
標籤:
