错误TypeError:data.sort不是函数

时间:2019-08-26 09:39:57

标签: angular angular-material

我正在使用Angular材质版本8.0.3。加载组件时出现以下错误。

ERROR TypeError: data.sort is not a function
    at MatTableDataSource.sortData (table.js:466)
    at MatTableDataSource._orderData (table.js:684)
    at MapSubscriber.project (table.js:633)
    at MapSubscriber._next (map.js:29)
    at MapSubscriber.next (Subscriber.js:49)
    at CombineLatestSubscriber.notifyNext (combineLatest.js:73)
    at InnerSubscriber._next (InnerSubscriber.js:11)
    at InnerSubscriber.next (Subscriber.js:49)
    at MergeMapSubscriber.notifyNext (mergeMap.js:69)
    at InnerSubscriber._next (InnerSubscriber.js:11)

以下是代码段:

@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort


ngOnInit() {
    this.dataSource = new MatTableDataSource(this.dataList);
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
}

1 个答案:

答案 0 :(得分:0)

尝试此代码。

dataSource = new MatTableDataSource(this.dataList);    
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort


ngOnInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}

this.dataSource = new MatTableDataSource(this.dataList);

应该在ngOnInit之外,并且this.dataSource应该只是

dataSource = new MatTableDataSource(this.dataList);