Angular5 - TypeError:无法读取undefined的属性“template”

时间:2017-12-07 10:15:39

标签: angular angular-material angular-material2 angular5

我使用Angular material@5.0.0-rc.2为mat-table中的每一行添加了EDIT,DELETE和DETAILS按钮。 所有按钮都正常工作但是,   *我收到错误“无法读取未定义的属性'模板'和   *对于每个按钮单击,所有内容都显示在同一页面上

itemlist.component.html

<mat-table #table [dataSource]="dataSource">
  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
    <mat-cell *matCellDef="let row"> {{row.name}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="description">
    <mat-header-cell *matHeaderCellDef> Description </mat-header-cell>
    <mat-cell *matCellDef="let row"> {{row.description}} </mat-cell>
  </ng-container>

  <ng-container matColumnDef="actions">
    <mat-cell *matCellDef="let row">
      <button mat-button (click)="showDetails(row)">DETAILS</button>
      <button mat-button (click)="editItem(row)">EDIT</button>
      <button mat-button (click)="deleteItem(row)">DELETE</button>
    </mat-cell>
  </ng-container>

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

itemlist.component.ts

export class ItemListComponent {
  @Input() dataSource;
  displayedColumns = ['name', 'description', 'actions'];

  @Input() items: Item[];
  @Output() onEdit = new EventEmitter<Item>();
  @Output() onShow = new EventEmitter<Item>();
  @Output() onDelete = new EventEmitter<Item>();

  itemsTrackByFn = (index: string, item: Item) => item.id;

  showDetails(item: Item) {
    this.onShow.emit(item);
  }
  editItem(item: Item) {
    this.onEdit.emit(item)
  }
  deleteItem(item: Item) {
    this.onDelete.emit(item)
  }
}

itemindex.component.html

<app-item-list [dataSource]="dataSource"
              (onShow)="showItem($event)"
              (onDelete)="deleteItem($event)"
              (onEdit)="editItem($event)"
></app-item-list>

itemindex.component.ts

export class ItemIndexComponent implements OnInit {
  items$: Observable<Item[]>;
  public dataSource: ItemsDatasource;

  constructor(public store: Store<fromRoot.State>, private router: Router){}

  ngOnInit() {
    this.items$ = this.store.select(fromItems.getAllItems);
    this.store.dispatch(new itemsActions.LoadAll());
    this.dataSource = new ItemsDatasource(this.items$);
  }

  editItem(item: Item) {
    this.store.dispatch(new itemsActions.SetCurrentItemId(item.id));
    this.router.navigate(['/items', item.id, 'edit'])
  }
  showItem(item: Item) {
    this.store.dispatch(new itemsActions.SetCurrentItemId(item.id));
    this.router.navigate(['/items', item.id])
  }
  deleteItem(item: Item) {
     this.store.dispatch(new itemsActions.Delete(item.id));
   }
 }
}


export class ItemsDatasource extends DataSource<Item> {

  public constructor(private items$: Observable<Item[]>) {
    super()
  }

  public connect(collectionViewer: CollectionViewer): Observable<Item[]> {
    return this.items$;
  }

  public disconnect(collectionViewer: CollectionViewer): void {}
}

任何建议都会有所帮助

6 个答案:

答案 0 :(得分:31)

我忘了为动作定义标题单元格。所以它抛出了这个错误。这是解决这个问题的代码。

<ng-container matColumnDef="actions">
  <mat-header-cell *matHeaderCellDef></mat-header-cell>
  <mat-cell *matCellDef="let row">
    <button mat-button (click)="showDetails(row)">DETAILS</button>
    <button mat-button (click)="editItem(row)">EDIT</button>
    <button mat-button (click)="deleteItem(row)">DELETE</button>
  </mat-cell>
</ng-container>

答案 1 :(得分:4)

我遇到了此错误,因为我忘记在其中添加thtd ng-container

<ng-container matColumnDef="actions">

</ng-container>

答案 2 :(得分:1)

检查.ts文件中的配置

displayedColumns = ['name', 'description', 'action'];
dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);

const ELEMENT_DATA: Element[] = [
    { name: '', description: '' },
    { name: '', description: '' }
]

答案 3 :(得分:1)

我的解决方案是在 ng-container 元素内添加 mat-cell 元素:

原始代码是:

  <ng-container matColumnDef="testColumn">
    <mat-header-cell *matHeaderCellDef>testColumn</mat-header-cell>
  </ng-container>

修复它的代码更新:

  <ng-container matColumnDef="testColumn">
    <mat-header-cell *matHeaderCellDef>testColumn</mat-header-cell>
    <mat-cell *matCellDef="let element">{{ element.testColumn}}</mat-cell>
  </ng-container>

答案 4 :(得分:0)

我有同样的问题。我不小心使用了* matCelLDef而不是* matCellDef。

答案 5 :(得分:0)

对我来说这是缺少的页脚单元

<td mat-footer-cell *matFooterCellDef > </td >

添加此修复它。