MatPaginator gets undefined

2020-07-11 07:20发布

I have replicated thsi case: Angular 5 Material Table not getting data from service

But when I try access any property from paginator I get undefined error from this objet.

Any ideas?

Thanks

7条回答
Lonely孤独者°
2楼-- · 2020-07-11 07:27

In some cases, the issue is related to conditional outer div. Ex:

<div *ngIf="condition">
    ...

   <mat-paginator ....>
</div>

For a such scenario just replace *ngIf="condition" with [hidden]="!condition" and it'll work. Please refer to https://github.com/angular/components/issues/10205 for more details

查看更多
smile是对你的礼貌
3楼-- · 2020-07-11 07:30

MatPaginator being undefined most likely means you do not have the module imported. Sometimes angular mat does not explicitly tell you these things that you are missing. But always check the API tab under their documentation before using a component. At your module level, you should have the following in your app.module.ts file.

the import

import { MatButtonModule, MatTableModule, MatPaginatorModule, MatProgressSpinnerModule, MatTableDataSource } from '@angular/material';

Your component imported that used paginator of course

import { TableComponent } from './table/table.component';

Those modules imported in your imports array

imports: [
    BrowserAnimationsModule,
    NgbModule.forRoot(),
    FormsModule,
    RouterModule,
    AppRoutingModule,
    CommonModule,
    MatButtonModule,
    MatTableModule,
    BrowserAnimationsModule,
    MatPaginatorModule,
    MatProgressSpinnerModule,
    HttpClientModule
],

And those modules exported if necessary (different topic so I wont discuss here).

exports: [
    MatButtonModule,
    MatTableModule,
    MatPaginatorModule
],

This is all happening in my App.Module

export class AppModule { }

This assuming you do bot have your project structured as feature modules. In that case you would really only need everything I talked about in the module in which your component lives. But in this case, where everything is under the app module, this works just fine.

查看更多
老娘就宠你
4楼-- · 2020-07-11 07:33

double check you have paginator component in your html

<mat-paginator [pageSizeOptions]="[50, 100, 200]" [length]="resultsLength" showFirstLastButtons></mat-paginator>

查看更多
孤傲高冷的网名
5楼-- · 2020-07-11 07:44
<div class="  fixed-form-bottom" [hidden]="changeState">
  <div class="team-footer">
    <mat-paginator #paginator [pageSize]="pageSize" [showFirstLastButtons]="true" [length]="totalSize"
      [pageIndex]="currentPage" (page)="pageEvent = handlePage($event)" [hidePageSize]="true">
    </mat-paginator>
  </div>
</div>

And in .ts file , you compare your condition and pass true/ false from there

if(this.totalSize>3){
  this.changeState=true;

}

its worked for me

查看更多
Ridiculous、
6楼-- · 2020-07-11 07:47

"Inserting an *ngIf means that its contents cannot be queried until the view is initialized with change detection. This means the query for sort is empty at ngOnInit.

This can be resolved by changing the setup from ngOnInit to ngAfterViewInit. https://stackblitz.com/edit/angular-mewfek?file=src/app/app.component.ts "

reference https://github.com/angular/components/issues/15966

查看更多
forever°为你锁心
7楼-- · 2020-07-11 07:49

I got the same issue. Placing mat-paginator tag outside *ngIf resolved my issue. Make sure it is available to component class without any conditions.

查看更多
登录 后发表回答