Home > Software design >  Property 'table' has no initializer and is not definitely assigned in the constructor
Property 'table' has no initializer and is not definitely assigned in the constructor

Time:07-13

I want following this link

https://javascript.plainenglish.io/beautiful-styling-for-drag-and-drop-rows-in-the-angular-datatable-6870768c5a8f

to create a Drag Drop in Material table.

app.component.ts:

import { Component,ViewChild } from '@angular/core';
import { MatTable } from '@angular/material/table';
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';

export interface PeriodicElement {
  name: string;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {name: 'Helium', weight: 4.0026, symbol: 'He'},
  {name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {name: 'Boron', weight: 10.811, symbol: 'B'},
  {name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  @ViewChild('dataTable') table: MatTable<PeriodicElement>;
  displayedColumns: string[] = ['name', 'weight', 'symbol'];
  dataSource = ELEMENT_DATA;

  dropTable(event: CdkDragDrop<PeriodicElement[]>): void {
    moveItemInArray(this.dataSource, event.previousIndex, event.currentIndex);
    this.table.renderRows();
  }
}

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { MatTableModule } from '@angular/material/table'
// import { FlexLayoutModule } from '@angular/flex-layout';
import {DragDropModule} from '@angular/cdk/drag-drop';
import {CommonModule} from '@angular/common';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';

import { AppComponent } from './app.component';

@NgModule({
  imports:      [
    CommonModule,
    BrowserModule,
    BrowserAnimationsModule,
    DragDropModule,
    MatTableModule,
    // FlexLayoutModule,
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

app.component.html

<div>
  <!-- <div fxFlex="5"></div>
  <div fxFlex="50" > -->
    <table mat-table
       #dataTable
       [dataSource]="dataSource"
       cdkDropList
       (cdkDropListDropped)="dropTable($event)">

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

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

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

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

    </table>
  </div>
  <!-- <div fxFlex="45"></div> -->
<!-- </div> -->

Getting this error :

Property 'table' has no initializer and is not definitely assigned in the constructor.

30 @ViewChild('dataTable') table: MatTable;

CodePudding user response:

Table is not initialized, only typed for the moment. So to clean this error, you can add a "!" to the property to say to the linter: " Hey, i know it does not hav a value yet, but it will be done later.

@ViewChild('dataTable') table!: MatTable<PeriodicElement>;

CodePudding user response:

You can also put the strict parameter to false in your config.json at the root of the project

  • Related