You'll also get this error if your target element is inside a hidden element. If this is your HTML:
<div *ngIf="false">
<span #sp>Hello World</span>
</div>
Your @ViewChild('sp') sp
will be undefined.
In such a case, then don't use *ngIf
.
Instead use a class to show/hide your element being hidden.
<div [class.show]="shouldShow">...</div>