【发布时间】:2019-07-19 21:31:55
【问题描述】:
我的 mat-table 工作正常,但是在按照官方 api 文档添加 mat-sort 时,它在 ngAfterViewInit 失败并显示以下消息
无法设置未定义的属性“排序” aq LeadsComponent.push../src/app/leads/leads.component.ts.LeadsComponent.ngAfterViewInit
已经有一个关于 Mat-table Sorting Demo 的 SO 帖子并尝试了它们,但我仍然无法让它工作。
有人能帮我解决这个问题吗?官方示例使用组件本身中定义的“静态”MatTableDataSource,但是我从后端查询。
MatSortModule 已经导入 app.module.ts,mat-sort-header 指令应用于列,ngAfterViewInit 已经与官方示例中的完全一样...
export class LeadsComponent implements OnInit,AfterViewInit {
displayedColumns: string[] = ['name', 'leadStatus', 'mobile', 'email', 'actions'];
dataSource: MatTableDataSource<LeadsChildObject>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(public dialog: MatDialog, private dashBoardService: LeadsService,
private toast: ToastrService) {
}
ngAfterViewInit() {
this.dataSource.sort = this.sort;
}
ngOnInit() {
this.dashboardService.getFeedback.subscribe(
res => {
this.leadlist= res;
this.dataSource = new MatTableDataSource(this.leadlist);
}
);
}
}
}
【问题讨论】:
-
你在哪里初始化
this.dataSource? -
我更新了我的代码,请立即查看
标签: angular angular-material angular-material-table