简体   繁体   中英

Error when trying to use ViewChild annotation in Angular

I am trying to use ViewChild like this:

@ViewChild('hello', { static: false }) 
divHello?: ElementRef;

This is the template:

<div class="wrap" #hello>
Some test data
</div>

But, the ViewChild annotation is showing this error:

Unable to resolve signature of property decorator when called as an expression.
  This expression is not callable.
    Type 'void' has no call signatures.

Making the target ES5 is not my solution, since I don't want that in my case.

Is there any way to use ViewChild? I was referring to this tutorial: https://www.tektutorialshub.com/angular/renderer2-angular/

In my case VSCode created a ViewChild function somehow (automatically? when I copy-pasted the @ViewChild annotation?). So check your code if there is a ViewChild function in you ts file and delete it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM