Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @ViewChild in Angular `strict`mode

What is the common way to use @ViewChild in Angular strict mode?

For example, when I take the code for sorting a Material table as described in documentation

@ViewChild(MatSort) sort: MatSort;

The compiler says:

error TS2564: Property 'sort' has no initializer and is not definitely assigned in the constructor.

As a possible solution I could use

@ViewChild(MatSort, { static: false }) sort!: MatSort;

... but that seems to me more like a workaround.

like image 401
A. Sa. Avatar asked Oct 25 '25 12:10

A. Sa.


1 Answers

It appears to be correct to remomve the { static: false } part. So a valid declaration for sort would be:

@ViewChild(MatSort) sort!: MatSort;
like image 98
A. Sa. Avatar answered Oct 28 '25 00:10

A. Sa.