在我的 Angular 应用程序中,我的 @ViewChild 实例无法填充 HTL matSort。
我的组件.ts:
import { MatSort } from '@angular/material';
export class MyClassComponent {
@ViewChild(MatSort) sort: MatSort;
}
ngOnInit() {
console.log(this.sort); // undefined
}
我的组件.html:
<mat-table #table [dataSource]="dataSource" matSort>
.......................................................
.......................................................
</mat-table>
我需要使用 matSort 中的 sortChange 但它总是返回未定义。
它将被初始化并可在AfterViewInit https://angular.io/api/core/AfterViewInit生命周期挂钩:
export class MyClassComponent implements AfterViewInit {
@ViewChild(MatSort) sort: MatSort;
ngAfterViewInit() {
console.log(this.sort); // MatSort{}
}
}
有关生命周期挂钩的更多信息:https://angular.io/guide/lifecycle-hooks https://angular.io/guide/lifecycle-hooks.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)