Avatar ·

Angular @ViewChild Error: Expected 2 arguments, but got 1 — how to fix?

Hi! I'm migrating from Angular 7 to Angular 9 and encountered a confusing error. Previously I used `@ViewChild()` like this: ```typescript @ViewChild('myDiv') myDiv: ElementRef; ``` But now the TypeScript compiler gives an error: ``` Expected 2 arguments, but got 1. ``` I read that in Angular 8+ the signature of `@ViewChild` changed. I tried adding `{ static: false }` — the error disappeared. But I don't fully understand what this means. When should I use `static: true`, and when `static: false`? For example, I have a template with an element `
`, and I want to access it in `ngAfterViewInit()`. Also, there is a directive ``, which I access in `ngOnInit()`. Please explain how to properly use `@ViewChild` in newer versions of Angular. Why is the second argument required? And what happens if I set `static: true` everywhere? Thanks! <<>>

Reason for the error and correct usage of @ViewChild in Angular