问题描述: 在Angular中使用mat-table组件时,自定义数据源时出现了错误“undefined MatSort”。
解决方法:
import { MatSort } from '@angular/material/sort';
@ViewChild(MatSort) sort: MatSort;
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}
import { MatSortable } from '@angular/material/sort';
export class CustomDataSource extends DataSource implements MatSortable {
// ...
sortData(sort: MatSort) {
// 实现排序逻辑
}
}
通过上述步骤,就可以解决“Angular mat-table自定义数据源undefined MatSort”的错误。