Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 无法加载角度材质数据表组件的预定义数据_Angular_Angular Material - Fatal编程技术网

Angular 无法加载角度材质数据表组件的预定义数据

Angular 无法加载角度材质数据表组件的预定义数据,angular,angular-material,Angular,Angular Material,我用ng g@Angular/Material:Material table-name=skill table创建了一个预定义的角度材质数据表。为了测试我是否可以显示表,我想在我的一个组件中显示表。但我有一个错误。它告诉我我的数据没有定义,但应该定义。然后我做了一些研究,发现了一些可以帮助我的东西。我尝试了所有的选择,但这并没有真正的帮助 这是我的错误: ERROR TypeError: Cannot read property 'data' of undefined at Object

我用ng g@Angular/Material:Material table-name=skill table创建了一个预定义的角度材质数据表。为了测试我是否可以显示表,我想在我的一个组件中显示表。但我有一个错误。它告诉我我的数据没有定义,但应该定义。然后我做了一些研究,发现了一些可以帮助我的东西。我尝试了所有的选择,但这并没有真正的帮助

这是我的错误:

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>
这是我的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);
}

所以我的问题是,我如何定义我的数据或只是修复这个错误?

您得到这个错误,因为您的数据源最初是空的。因此,您可以将例如*ngIf添加到mat paginator组件:

<mat-paginator *ngIf="dataSource"
  #paginator
  [length]="dataSource.data.length"
  [pageIndex]="0"
  [pageSize]="50"
  [pageSizeOptions]="[25, 50, 100, 250]"
>
您应该在ngOnInit而不是ngAfterViewInit中初始化数据源

修改component.ts:

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

查看以更好地了解Angular lifecycle挂钩是如何工作的,以及它们何时生效。

@despamigros您可以发布您收到的此错误吗?这只是一种解决方法,必然会产生更多错误。但是如果你真的想隐藏分页器,我建议使用[hidden]=!取而代之的是数据源。这样可以确保分页器已初始化,从而避免任何其他错误
ngOnInit() {
    this.dataSource = new SkillTableDataSource(this.paginator, this.sort);
}