angular - 如何将 Observable 转换为 MatTableDataSource?
问题描述
我有如下代码,但不知道如何将可观察的“帖子”从 firebase 转换为 MatTableDataSource“postsTable”?
由于我想将 mat-table 数据源更改为 MatTableDataSource,所以使用 mat-paginator
我试图在定义中添加一个新的 MatTableDataSource 或在从 firestore 返回但无法正常工作的数据映射之后创建它。
export interface Post {
title: string;
category: string;
image: string;
content: string;
updateAt: firebase.firestore.FieldValue;
}
export interface PostID extends Post { id: string; }
export class PostsViewComponent implements OnInit {
private postCollection: AngularFirestoreCollection;
posts: Observable<PostID[]>;
postsTable = new MatTableDataSource();
ngOnInit() {
this.postCollection = this.angularFirestore.collection('posts');
this.posts = this.postCollection.snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data() as PostID;
data.id = a.payload.doc.id;
return data;
}))
);
}
}
解决方案
推荐阅读
- php - 致命错误:无法重新声明先前声明的函数()
- typescript - 在 TypeScript 中,有没有办法创建一个泛型 mixin 函数,它还允许在返回的类上使用泛型类型参数?
- java - 基于对象类覆盖java中的打印语句
- javascript - 将一维二进制数组拆分为仅由连续一维组成的二维数组
- python - 使用蒙特卡罗方法评估多变量的多重积分
- flutter - 在这种情况下,在颤振中构建 .apk 文件有什么问题?
- matplotlib - 重置(或赋值)到 matplotlib 中的 RadioButton
- c# - 根据列表内容显示/隐藏 DataGrid 列
- java - 为特定 DBMS(数据库引擎)动态设置 DataSource 的正确方法是什么?
- html - 如何匹配 HTML 输入并使用 CSS 选择样式