Angular @ViewChild() error: Expected 2 arguments, but got 1

后端 未结 11 2659
时光取名叫无心
时光取名叫无心 2020-11-29 18:10

When trying ViewChild I am getting the error. Error is \"An argument for \'opts\' was not provided.\"

Both @ViewChild is giving the error.



        
11条回答
  •  天涯浪人
    2020-11-29 18:37

    In Angular 8, ViewChild always takes 2 param, and second params always has static: true or static: false

    You can try like this:

    @ViewChild('nameInput', {static: false}) component
    

    Also,the static: false is going to be the default fallback behaviour in Angular 9.

    What are static false/true: So as a rule of thumb you can go for the following:

    • { static: true } needs to be set when you want to access the ViewChild in ngOnInit.

      { static: false } can only be accessed in ngAfterViewInit. This is also what you want to go for when you have a structural directive (i.e. *ngIf) on your element in your template.

提交回复
热议问题