Angular 按列值对mat表进行排序

Angular 按列值对mat表进行排序,angular,columnsorting,mat-table,Angular,Columnsorting,Mat Table,我有一张有客户信息的垫子桌。我想按列值对行进行排序。没有错误,但排序不起作用 表格标签有matSort: <mat-table [dataSource]="dataSource" matSort> 在组件类中,我有 ... @ViewChild(MatSort) sort: MatSort; ... ngOnInit() { this.dataSource = new MatTableDataSource(this.data as any); this.dataSo

我有一张有客户信息的垫子桌。我想按列值对行进行排序。没有错误,但排序不起作用

表格标签有
matSort

<mat-table [dataSource]="dataSource" matSort>
在组件类中,我有

...
@ViewChild(MatSort) sort: MatSort;
...
ngOnInit() {
    this.dataSource = new MatTableDataSource(this.data as any);
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
}
所以每件事都很像在哪个州

如果将
MatTableDataSource
用于表的数据源, 向数据源提供
MatSort
指令,它将 自动侦听排序更改并更改数据顺序 由表格呈现

但是样式和排序功能都没有应用到表中

不相关,因为即使在课堂上硬编码了我的数据,问题仍然存在

我不知道我错过了什么。
问题的根源是逃避我。您可以查看我的完整代码并运行

查看您的堆栈闪电战,
MatSort
模块不是
mat.module.ts
文件中的
导出
。纠正

因此,对于发现相同问题的其他人,代码使用单个模块来选择应用程序应该可用的所有材料模块。应用程序中的任何其他模块只需引用此单个模块,而不需要引用每个材料模块本身

但是,为了实现这一点,这个单一模块(
mat.module.ts
)需要
导出导入时想要向其他人公开的任何内容。在这种情况下,它是从材料中导入的任何东西

因此,解决办法是:

@NgModule({
进口:[
公共模块,
//…其他模块
Matsort模块,
//…其他模块
],
出口:[
//…其他模块
Matsort模块//
...
@ViewChild(MatSort) sort: MatSort;
...
ngOnInit() {
    this.dataSource = new MatTableDataSource(this.data as any);
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
}