ERROR TypeError: Cannot read properties of undefined (reading 'nativeElement') while using datatable

Viewed 30

I am getting ERROR TypeError: Cannot read properties of undefined (reading 'nativeElement') while using the datatable.

typescript

 @ViewChild('dataTable',{static:false}) table;

  ngAfterViewInit(): void {
      // if (!this.table) return;    
    this.dataTable=$(this.table.nativeElement);
    this.dataTable.DataTable();
    console.log( this.dataTable.DataTable());
    
  }

HTML

<table #dataTable id="example" class="table table-striped table-bordered" style="width:100%"> 

  </table>
1 Answers
 @ViewChild('dataTable',{static:false}) table : ElementRef;

  ngAfterViewInit(): void {
      // if (!this.table) return;    
    this.dataTable=$(this.table.nativeElement);
    this.dataTable.DataTable();
    console.log( this.dataTable.DataTable());
    
  }

Or you can use @ViewChild('dataTable',{static:false}) table : any;

Related