Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 cannot find name 'ViewChild'

I have this strange error and didn't find anything similar to it .. I'm using ng2-canvas-whiteboard component for a drawing canvas on my website, everything's working perfectly except when i try to get reference to that canvas using :

@ViewChild('canvasWhiteboard') canvasWhiteboard: CanvasWhiteboardComponent;

as the official docs suggested, i'm getting this error :

Failed to compile.

e:/frontend/src/app/components/user/dashboard/dashboard.component.ts (15,2): Cannot find name 'ViewChild'.

Any suggestions ? Thank you

like image 285
Soufiane Touil Avatar asked Oct 18 '17 18:10

Soufiane Touil


People also ask

What is the difference between ViewChild () and ContentChild ()?

ViewChild is used to select an element from component's template while ContentChild is used to select projected content.

What is the use of annotation @ViewChild?

The @ViewChild decorator allows us to inject into a component class references to elements used inside its template, that's what we should use it for. Using @ViewChild we can easily inject components, directives or plain DOM elements.

What is ViewChild in Angular?

A ViewChild is a component, directive, or element as a part of a template. If we want to access a child component, directive, DOM element inside the parent component, we use the decorator @ViewChild() in Angular.

What is the difference between ViewChild and ViewChildren?

Working with @ViewChildren is similar to @ViewChild, but the difference between the two is @ViewChildren provides a list of element references rather than returning a single reference. It is used to reference multiple elements. We can then iterate the list of the element referenced by the variable.


1 Answers

You forgot to import ViewChild before using it

import { ViewChild } from '@angular/core'
like image 156
Pankaj Parkar Avatar answered Sep 28 '22 12:09

Pankaj Parkar