How to fix Angular 9 @ViewChild Expected 2 arguments, but got 1: An argument for 'opts' was not provided.
Problem:
You are trying to compile your Angular 9.x application, but you see an error message like
app/my-component/my-component.component.ts:24:4 - error TS2554: Expected 2 arguments, but got 1.
24 @ViewChild(MyOtherComponent) myOtherComponent: MyOtherComponent;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../node_modules/@angular/core/core.d.ts:7888:47
7888 (selector: Type<any> | Function | string, opts: {
~~~~~~~
7889 read?: any;
~~~~~~~~~~~~~~~~~~~
7890 static: boolean;
~~~~~~~~~~~~~~~~~~~~~~~~
7891 }): any;
~~~~~
An argument for 'opts' was not provided.
Solution
Find this line in your code at the location specified in the error message:
@ViewChild(MyOtherComponent) myOtherComponent: MyOtherComponent;
and add
{static: false}
as second argument to the @ViewChild()
declaration:
@ViewChild(MyOtherComponent, {static: false}) myOtherComponent: MyOtherComponent;
In most cases, you want to useĀ static: false
. See this post on StackOverflow for details on when to use static: true
as opposed to static: false
.