角材料分页器未定义

时间:2018-12-12 10:37:03

标签: angular typescript angular-material paginator

我使用角度材质(MatTable,MatPaginator)。 当我登录到我的应用程序并加载了组件视图时,在浏览器调试器中出现以下错误:

    ERROR TypeError: "_this._paginator is undefined; can't access its "pageIndex" property"

    ExampleDataSource http://localhost:4200/main.js:1446
    __tryOrUnsub http://localhost:4200/vendor.js:160732
    next http://localhost:4200/vendor.js:160670
    _next http://localhost:4200/vendor.js:160603
    next http://localhost:4200/vendor.js:160578
    _subscribe http://localhost:4200/vendor.js:159751
    _trySubscribe http://localhost:4200/vendor.js:159963
    _trySubscribe http://localhost:4200/vendor.js:160384

我的目标是摆脱这个错误。

由于可能导致此错误的代码过多,请在我的Github上查看我的组件:https://github.com/chrs-k/Behaeltermanagement/tree/master/Behaeltermanagement/client/src/app/table

问题必须位于table.component.ts

非常感谢您!

1 个答案:

答案 0 :(得分:1)

Paginator标有@ViewChild,因此只有在调用ngAfterViewInit()之前才会加载。但是,您将在ngOnInit中调用的loadData()中放置了一个构造新ExampleDataSource的实例。 但是根据Angular生活方式钩子方法的顺序,ngOnInit()将在ngAfterViewInit()之前调用。您已经在ngAfterViewInit()之前调用了至少两次!因此,将this.loadData()放入ngAfterViewInit,然后您应该具有

dataSource: ExampleDataSource;

删除所有依赖this.paginator的内容,而将其放在ngAfterViewInit中。

相关问题