如何将Observable转换为MatTableDataSource?

时间:2019-02-08 06:24:43

标签: angular observable

我有以下代码,但不知道如何将可观察到的“帖子”从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;
            }))
        );
     }

}

0 个答案:

没有答案