AngularFire - 如何在集合查询中显示Firestore键和值
创始人
2024-10-24 19:01:43
0

要在AngularFire中显示Firestore集合查询的键和值,可以使用snapshotChanges()方法来获取集合的快照,并使用map()操作符来转换快照中的每个文档。

首先,确保已经安装并导入了angularfire2rxjs依赖项。

接下来,在你的组件中,导入必要的AngularFire和rxjs操作符:

import { Component } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

然后,创建一个接口来定义你的Firestore文档的模型:

interface Item {
  key: string;
  value: string;
}

接下来,在组件类中,创建一个itemsCollection属性来存储Firestore集合的引用:

@Component({
  selector: 'app-items',
  templateUrl: './items.component.html',
  styleUrls: ['./items.component.css']
})
export class ItemsComponent {
  private itemsCollection: AngularFirestoreCollection;
  items: Observable;

  constructor(private afs: AngularFirestore) {
    this.itemsCollection = this.afs.collection('items');
    this.items = this.itemsCollection.snapshotChanges().pipe(
      map(actions => {
        return actions.map(a => {
          const data = a.payload.doc.data() as Item;
          const key = a.payload.doc.id;
          return { key, ...data };
        });
      })
    );
  }
}

在构造函数中,我们使用this.afs.collection('items')创建了一个对'items'集合的引用。然后,我们使用snapshotChanges()方法获取集合的快照,并使用map()操作符将快照中的每个文档转换为我们定义的Item接口。

map()操作符中,我们使用a.payload.doc.data()来获取文档的数据,使用a.payload.doc.id来获取文档的键。最后,我们返回一个包含键和数据的对象。

最后,在模板中,我们可以使用async管道来订阅items观察者,并显示每个文档的键和值:

  • {{ item.key }}: {{ item.value }}

这样,我们就可以在Angular中使用AngularFire显示Firestore集合查询的键和值了。

相关内容

热门资讯

透视真的!wepoker辅助器... 透视真的!wepoker辅助器怎么弄,wepoker俱乐部辅助(透视)大神讲解(有挂揭秘);1、we...
透视攻略!wepoker私人局... 透视攻略!wepoker私人局有透视吗,wepoker辅助器有哪些功能(透视)科技教程(有挂介绍)1...
透视挂透视!wepoker有什... 透视挂透视!wepoker有什么规律,wejoker辅助机器人(透视)教你教程(有挂解说);1、每一...
透视挂透视!wejoker私人... 透视挂透视!wejoker私人辅助软件,wepoker好友助力码(透视)我来教教你(有挂教程)运we...
透视挂!wepoker破解工具... 透视挂!wepoker破解工具,wepoker私人局俱乐部怎么进(透视)扑克教程(有挂插件)1、游戏...
透视挂!wepoker私人局有... 透视挂!wepoker私人局有透视吗,wepoker祈福有用吗(透视)必备教程(有挂黑科技)1、下载...
透视软件!wepoker透视器... 透视软件!wepoker透视器免费,wepoker线上大神(透视)科技教程(有挂插件)wepoker...
辅助透视!wepoker透视脚... 辅助透视!wepoker透视脚本免费下载pc,黑侠破解wepoker(透视)必备教程(有挂方法);1...
透视系统!wepoker私人辅... 透视系统!wepoker私人辅助器,wepoker有没有机器人(透视)AI教程(有挂攻略)1、wep...
透视玄学!htx矩阵wepok... 透视玄学!htx矩阵wepoker辅助,wepoker怎么挂飞机(透视)细节方法(有挂脚本)亲,关键...