首次加载页面时,Angular material 分页器中的设置 pageIndex 不工作
Set pageIndex in Angular material paginator not working when page loaded for first time
我想要 Angular 的分页器在页面刷新时从第二页开始,但我遇到了这个错误
ERROR TypeError: Cannot set property 'pageIndex' of undefined
我尝试使用延迟,但这不是一个好的解决方案,
HTML
<mat-paginator #paginator
showFirstLastButtons
[length]="length"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
(page)="pageEvent = changePage($event)">
</mat-paginator>
TypeScript
@ViewChild('paginator', { static: true }) paginator;
pageSize = 5;
pageSizeOptions: number[] = [5, 10, 50, 100];
pageEvent: PageEvent;
buttonDisablity = true;
ngOnInit(): void {
this.paginator.pageIndex = 2;
}
ngOnInit
挂钩无法访问 viewchild
实例。 ViewChild
实例仅在 afterViewInit
钩子中或之后可用。
ngAfterViewInit(): void {
this.paginator.pageIndex = 2;
}
我想要 Angular 的分页器在页面刷新时从第二页开始,但我遇到了这个错误
ERROR TypeError: Cannot set property 'pageIndex' of undefined
我尝试使用延迟,但这不是一个好的解决方案,
HTML
<mat-paginator #paginator
showFirstLastButtons
[length]="length"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
(page)="pageEvent = changePage($event)">
</mat-paginator>
TypeScript
@ViewChild('paginator', { static: true }) paginator;
pageSize = 5;
pageSizeOptions: number[] = [5, 10, 50, 100];
pageEvent: PageEvent;
buttonDisablity = true;
ngOnInit(): void {
this.paginator.pageIndex = 2;
}
ngOnInit
挂钩无法访问 viewchild
实例。 ViewChild
实例仅在 afterViewInit
钩子中或之后可用。
ngAfterViewInit(): void {
this.paginator.pageIndex = 2;
}