关于angular:MatSort无法正常工作。 引发错误:必须使用MatSort指令将MatSortHeader放置在父元素中

MatSort not working. Throws Error: MatSortHeader must be placed within a parent element with the MatSort directive

我在matHeaderCellDef上指定mat-sort-header属性以在Angular Material中创建Sortable表后,出现以下错误

MatSortHeader must be placed within a parent element with the MatSort directive.

1
2
3
4
5
6
7
<mat-table #table matSort [dataSource]="myHttpDataSource">
....
<ng-container matColumnDef="myColumnName">
<mat-header-cell *matHeaderCellDef mat-sort-header></mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.somedetails}} </mat-cell>
</ng-container>
</mat-table>

任何指针/帮助表示赞赏


将'matSort'属性添加到mat-table

1
2
<mat-table #table [dataSource]="dataSource" matSort>
</mat-table>


1
2
3
4
5
6
<mat-table mat-table [dataSource]="dataSource" matSort>

    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
      <td mat-cell *matCellDef="let element"> {{element.name}} </td>
    </ng-container>
1
2
3
To add sorting behavior to the table, add the matSort directive to the
table and add mat-sort-header to each column header cell that should
trigger sorting.


您能给我们更多信息吗?

你有没有尝试去赶活动?

1
<mat-table #table [dataSource]="dataSource" matSort (matSortChange)="sortData($event)">

意识到我在另一个导致该问题的垫子表中使用了旧的mdSort标签。 更改为matSort后,问题已解决。