Не удается прочитать свойство 'pipe' из undefined при использовании Mat-Paginator - PullRequest
1 голос
/ 16 апреля 2020

Я использую шаблон ngx-admin, но я хочу добавить в Mat-Table и Mat-Paginator, поэтому я импортирую модуль @ angular / material. Ниже приведен мой код:

транзакция.module.ts

....
import { 
  MatTooltipModule,
  MatRippleModule,
  MatButtonModule, 
  MatSelectModule,
  MatFormFieldModule, 
  MatInputModule, 
  MatIconModule, 
  MatTableModule, 
  MatSortModule, 
  MatPaginatorModule, 
} from '@angular/material';

@NgModule({
  imports: [
    CommonModule,
    NbCardModule,
    NbIconModule,
    NbInputModule,
    ThemeModule,
    TransactionRoutingModule,
    NbInputModule,
    NbCardModule,
    NbButtonModule,
    NbActionsModule,
    NbUserModule,
    NbCheckboxModule,
    NbRadioModule,
    NbDatepickerModule,
    NbSelectModule,
    NbIconModule,
    ngFormsModule,
    ReactiveFormsModule,
    MatTooltipModule,
    MatRippleModule,
    MatButtonModule,
    MatSelectModule,
    MatFormFieldModule,
    MatInputModule,
    MatIconModule,
    MatTableModule,
    MatSortModule,
    MatPaginatorModule,
  ],
  declarations: [
    ...routedComponents,
  ],
})

транзакция.routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { TransactionComponent } from './transaction.component';
import { TransactionListComponent } from './list/transaction-list.component';
import { TransactionAddComponent } from './add/transaction-add.component';

const routes: Routes = [{
  path: '',
  component: TransactionComponent,
  children: [
    {
      path: 'list',
      component: TransactionListComponent,
    },
    {
      path: 'add',
      component: TransactionAddComponent,
    },
  ],
}];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class TransactionRoutingModule { }

export const routedComponents = [
    TransactionComponent,
    TransactionListComponent,
    TransactionAddComponent,
];

транзакция / список / транзакция-list.component.ts

  resultList: Transaction[] = [];
  dataSource: MatTableDataSource<Transaction>;
  displayedColumns: string[] = ['date', 'category_name', 'description', 'debit', 'credit'];

  @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  @ViewChild(MatSort, { static: true }) sort: MatSort;

  constructor(private transService: TransactionData) {}

  ngAfterViewInit() {
    this.transService.getTransactionData().subscribe(
      (res: Transaction[]) => {
        this.resultList = res;
        this.dataSource = new MatTableDataSource(this.resultList);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
      },
      err => {
        console.log(err);
      }
    )
  }

транзакция / список / транзакция-список. html

<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">
                <!-- Date Column -->
                <ng-container matColumnDef="date">
                  <th mat-header-cell *matHeaderCellDef mat-sort-header> Date </th>
                  <td mat-cell *matCellDef="let element"> {{ element.date | date: 'dd MMM yyyy' }} </td>
                </ng-container>

                <!-- Category Column -->
                <ng-container matColumnDef="category_name">
                  <th mat-header-cell *matHeaderCellDef mat-sort-header> Category </th>
                  <td mat-cell *matCellDef="let element"> {{ element.category_name }} </td>
                </ng-container>

                <!-- Description Column -->
                <ng-container matColumnDef="description">
                  <th mat-header-cell *matHeaderCellDef mat-sort-header> Description </th>
                  <td mat-cell *matCellDef="let element"> {{ element.description }} </td>
                </ng-container>

                <!-- Debit Column -->
                <ng-container matColumnDef="debit">
                  <th mat-header-cell *matHeaderCellDef class="text-right"> Debit </th>
                  <td mat-cell *matCellDef="let element" class="text-right"> {{ (element.type =='Out') ? (element.amount | number: '1.2-2') : ''}} </td>
                </ng-container>

                <!-- Credit Column -->
                <ng-container matColumnDef="credit">
                  <th mat-header-cell *matHeaderCellDef class="text-right"> Credit </th>
                  <td mat-cell *matCellDef="let element" class="text-right">  {{ (element.type =='In') ? (element.amount | number: '1.2-2') : ''}} </td>
                </ng-container>

                <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
                <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
              </table>
              <div class="pagination-div">
                <mat-paginator [pageSize]="10" [pageSizeOptions]="[5, 10, 25, 50]"></mat-paginator>
              </div>

Таблица работает нормально, однако селектор страниц пагинатора не может выбрать. И это выдает мне ошибку, как показано ниже:

ERROR TypeError: Cannot read property 'pipe' of undefined
    at MatSelect.ngAfterContentInit (select.js:678)
    at callHook (core.js:3937)
    at callHooks (core.js:3901)
    at executeInitAndCheckHooks (core.js:3842)
    at refreshView (core.js:11819)
    at refreshDynamicEmbeddedViews (core.js:13142)
    at refreshView (core.js:11800)
    at refreshDynamicEmbeddedViews (core.js:13142)
    at refreshView (core.js:11800)
    at refreshComponent (core.js:13217)
...