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

Hi,

I'm trying to do some stuff with a Grid (like a refresh after updating the datasource) but I'm unable to call the grid in the component's code.

Here's the code:

export class AppComponent implements OnInit {
  title = 'new-app';
  @ViewChild('grid'public gridGridComponent;

  ngOnInit(): void { }
}


This produces the following error:

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

Can you show me how to initialize the grid object?

BTW I tried with

@ViewChild('grid') public grid: GridComponent = {} as GridComponent;

but then when I do this.grid.refresh() I get an undefined error at runtime.

Attach is a basic code example of the problem.



Attachment: newapp_805dab08.zip

1 Reply 1 reply marked as answer

MS Manivel Sellamuthu Syncfusion Team August 3, 2021 01:06 PM UTC

Hi Adrian, 
 
Greetings from Syncfusion support. 
 
We have checked the attachment sample and we found that you have enabled strict mode. While enabling the strict mode compiler will throw the reported error(Property 'grid' has no initializer and is not definitely assigned in the constructor). It is not a grid related an issue. We suggest you to use the following the code example to overcome the mentioned problem. 
 
Please refer the below code example for more information. 
 
export class AppComponent implements OnInit { 
  title = 'new-app'; 
// make the property grid as optional 
  @ViewChild('grid', {statictrue})  
  public grid!GridComponent; 
 
 
 
The above mentioned way is suggested in the below general link. Please refer the below general link for more information. 
 
 
Please let us know if you need further assistance. 
 
Regards, 
Manivel 


Marked as answer
Loader.
Up arrow icon