首页 > 解决方案 > 如何将 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;
            }))
        );
     }

}

标签: angularobservable

解决方案


推荐阅读