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

后端 未结 11 2600
时光取名叫无心
时光取名叫无心 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:26

    In Angular 8, ViewChild has another param

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

    I resolved my issue like below

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

    0 讨论(0)
  • 2020-11-29 18:32

    Try this in angular 8.0:

    @ViewChild('result',{static: false}) resultElement: ElementRef;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 18:39

    That also resolved my issue.

    @ViewChild('map', {static: false}) googleMap;
    
    0 讨论(0)
  • 2020-11-29 18:45

    Use this

    @ViewChild(ChildDirective, {static: false}) Component

    0 讨论(0)
提交回复
热议问题