Using @ViewChild in Angular `strict`mode

Viewed 686

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.

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