Подтвердить что ты не робот

Angular5 - TypeError: Не удается прочитать шаблон свойства неопределенного

Я добавил кнопки EDIT, DELETE и DETAILS для каждой строки в мат-таблице, используя Angular [email protected] Все кнопки работают, но, * Я получаю сообщение об ошибке "Не могу прочитать шаблон свойства" неопределенного "и * для каждого нажатия кнопки, все отображается на одной странице

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 {}
}

Любое предложение было бы полезно

4b9b3361

Ответ 1

Я забыл определить ячейку заголовка для действий. Таким образом, это забрасывало эту ошибку. Вот код, который решил эту проблему.

<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>

Ответ 2

Проверьте конфигурацию в файле.ts

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

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

Ответ 3

Я получаю почти ту же ошибку, но в matRowDef:

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

Ошибка TypeError: Невозможно прочитать свойство 'template' из неопределенного в MatRowDef.extractCellTemplate

Файл TS

export class BankListComponent implements OnInit{
    dataSource = new BankDataSource(this.bankService);
  displayedColumns = ['ifsc', 'bank_id', 'branch', 'address', 'city', 'district', 'state', 'bank_name']

    constructor(private bankService: BankService){}
  ngOnInit(){

  }
}


export class BankDataSource extends DataSource<any>{
    constructor(private bankService: BankService){
        super();
    }
    connect(): Observable<Bank[]> {
        return this.bankService.getData();
    }
    disconnect() {}
}

'

App.mosule.ts Импортировать массив: -

'

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppRoutingModule } from './app-routing.module';
import {MatToolbarModule, MatTableModule } from '@angular/material';
import { HttpClientModule } from '@angular/common/http';

import { AppComponent } from './app.component';
import { AppHeaderComponent } from './header/app-header.component';
import { BankListComponent } from './banks/bank-list/bank-list.component';

import { BankService } from './banks/bank.service';

@NgModule({
  declarations: [
    AppComponent,
    AppHeaderComponent,
    BankListComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    MatTableModule,
    MatToolbarModule,
    BrowserAnimationsModule
  ],
  providers: [BankService],
  bootstrap: [AppComponent]
})

'