无法加载角形材料数据表组件的预定义数据

时间:2019-05-09 08:12:27

标签: angular angular-material

我用ng g @angular/material:material-table --name=skill-table制作了一个预定义的Angular Material数据表。为了测试是否可以显示表格,我想在我的组件之一中显示表格。但是我得到一个错误。它说我的数据没有定义,但是应该定义。然后我做了一些研究,发现了一些可以帮助我的东西。我尝试了所有选项,但这并没有真正的帮助。

这是我的错误:

ERROR TypeError: Cannot read property 'data' of undefined
    at Object.eval [as updateDirectives] (SkillTableComponent.html:26)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:23911)
    at checkAndUpdateView (core.js:23307)
    at callViewAction (core.js:23548)
    at execComponentViewsAction (core.js:23490)
    at checkAndUpdateView (core.js:23313)
    at callViewAction (core.js:23548)
    at execComponentViewsAction (core.js:23490)
    at checkAndUpdateView (core.js:23313)
    at callViewAction (core.js:23548)

这是我的html:

<mat-table
  class="full-width-table"
  #table
  [dataSource]="dataSource"
  matSort
  aria-label="Elements"
>
  <!-- Id Column -->
  <ng-container matColumnDef="id">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Id</mat-header-cell>
    <mat-cell *matCellDef="let row">{{ row.Id }}</mat-cell>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Name</mat-header-cell>
    <mat-cell *matCellDef="let row">{{ row.Name }}</mat-cell>
  </ng-container>

  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>

<mat-paginator
  #paginator
  [length]="dataSource.data.length"
  [pageIndex]="0"
  [pageSize]="50"
  [pageSizeOptions]="[25, 50, 100, 250]"
>
</mat-paginator>

这是我的component.ts文件:

@ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;
  dataSource: SkillTableDataSource;

  /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
  displayedColumns = ['id', 'name'];

  ngAfterViewInit() {
    this.dataSource = new SkillTableDataSource(this.paginator, this.sort);
  }

这是我的datasource.ts文件:

export interface SkillTableItem {
  name: string;
  id: number;
}

// TODO: replace this with real data from your application
const EXAMPLE_DATA: SkillTableItem[] = [
  {id: 1, name: 'Hydrogen'},
  {id: 2, name: 'Helium'},
  {id: 3, name: 'Lithium'},
  {id: 4, name: 'Beryllium'},
  {id: 5, name: 'Boron'},
  {id: 6, name: 'Carbon'},
  {id: 7, name: 'Nitrogen'},
  {id: 8, name: 'Oxygen'},
  {id: 9, name: 'Fluorine'},
  {id: 10, name: 'Neon'},
  {id: 11, name: 'Sodium'},
  {id: 12, name: 'Magnesium'},
  {id: 13, name: 'Aluminum'},
  {id: 14, name: 'Silicon'},
  {id: 15, name: 'Phosphorus'},
  {id: 16, name: 'Sulfur'},
  {id: 17, name: 'Chlorine'},
  {id: 18, name: 'Argon'},
  {id: 19, name: 'Potassium'},
  {id: 20, name: 'Calcium'},
];

/**
 * Data source for the SkillTable view. This class should
 * encapsulate all logic for fetching and manipulating the displayed data
 * (including sorting, pagination, and filtering).
 */
export class SkillTableDataSource extends DataSource<SkillTableItem> {
  data: SkillTableItem[] = EXAMPLE_DATA;

  constructor(private paginator: MatPaginator, private sort: MatSort) {
    super();
  }

  /**
   * Connect this data source to the table. The table will only update when
   * the returned stream emits new items.
   * @returns A stream of the items to be rendered.
   */
  connect(): Observable<SkillTableItem[]> {
    // Combine everything that affects the rendered data into one update
    // stream for the data-table to consume.
    const dataMutations = [
      observableOf(this.data),
      this.paginator.page,
      this.sort.sortChange
    ];

    // Set the paginator's length
    this.paginator.length = this.data.length;

    return merge(...dataMutations).pipe(map(() => {
      return this.getPagedData(this.getSortedData([...this.data]));
    }));
  }

  /**
   *  Called when the table is being destroyed. Use this function, to clean up
   * any open connections or free any held resources that were set up during connect.
   */
  disconnect() {}

  /**
   * Paginate the data (client-side). If you're using server-side pagination,
   * this would be replaced by requesting the appropriate data from the server.
   */
  private getPagedData(data: SkillTableItem[]) {
    const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
    return data.splice(startIndex, this.paginator.pageSize);
  }

  /**
   * Sort the data (client-side). If you're using server-side sorting,
   * this would be replaced by requesting the appropriate data from the server.
   */
  private getSortedData(data: SkillTableItem[]) {
    if (!this.sort.active || this.sort.direction === '') {
      return data;
    }

    return data.sort((a, b) => {
      const isAsc = this.sort.direction === 'asc';
      switch (this.sort.active) {
        case 'name': return compare(a.name, b.name, isAsc);
        case 'id': return compare(+a.id, +b.id, isAsc);
        default: return 0;
      }
    });
  }
}

/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
function compare(a, b, isAsc) {
  return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}

所以我的问题是,如何定义数据或仅解决此错误?

2 个答案:

答案 0 :(得分:0)

您应该在dataSource中初始化ngOnInit,而不是ngAfterViewInit

修改component.ts:

ngOnInit() {
    this.dataSource = new SkillTableDataSource(this.paginator, this.sort);
}

请参见this guide,以更好地了解Angular生命周期挂钩的工作原理以及何时生效。

答案 1 :(得分:-1)

由于您的dataSource最初为空,因此您收到此错误。因此,您可以将*ngIf添加到mat-paginator组件中:

<mat-paginator *ngIf="dataSource"
  #paginator
  [length]="dataSource.data.length"
  [pageIndex]="0"
  [pageSize]="50"
  [pageSizeOptions]="[25, 50, 100, 250]"
>