Cannot find name ‘Output’ – Angular
I was trying to learn and implement @Input and @Output decorators in Angular. And when I tried @Output, I got the following error Cannot find name ‘Output’ in Angular.
Cannot find name ‘Output’ – Angular
I was trying to use @Output decorator to enable the event “menuItemSelected” to listen to from outside of the HeaderComponent as shown below.
header.component.ts
import { Component, EventEmitter } from '@angular/core'; @Component({ selector:'app-header', templateUrl: './header.component.html' }) export class HeaderComponent { collapsed = true; @Output() menuItemSelected = new EventEmitter<string>(); onSelect(menuItem: string) { this.menuItemSelected.emit(); } }
Therefore, I have added @Output to the event property “menuItemSelected” to make it listenable from the parent component (for example, AppComponent).
header.component.html
<nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle" (click)="collapsed = !collapsed"> <span class="icon-bar" *ngFor="let iconBar of [1, 2, 3, 4]"></span> </button> <a routerLink="/" class="navbar-brand">Myrecipes.com</a> </div> <div class="navbar-collapse" [class.collapse]="collapsed" (window:resize)="collapsed = true"> <ul class="nav navbar-nav"> <li><a href="#" (click)="onSelect('recipes')">Recipes</a></li> <li><a href="#" (click)="onSelect('shopping-list')">Shopping List</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" role="button">Manage <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">Save Data</a></li> <li><a href="#">Fetch Data</a></li> </ul> </li> </ul> </div> </div> </nav>
AppComponent template
<app-header (menuItemSelected)="onNavigate($event)"></app-header> <div class="container"> <div class="row"> <div class="col-md-12"> <app-recipes *ngIf="loadedPage === 'recipes'"></app-recipes> <app-shopping-list *ngIf="loadedPage === 'shopping-list'"></app-shopping-list> </div> </div> </div>
I got the following error
ERROR in src/app/header/header.component.ts:12:5 - error TS2304: Cannot find name 'Output'. 12 @Output() menuItemSelected = new EventEmitter<string>(); ~~~~~~
Solution
Typescript could not figure out the @Outputdecorator and add the import statement.
Therefore, you need to add or modify the following import statement manually in your component class.
import { Component, EventEmitter, Output } from '@angular/core';
Now, the above import statement includes Output along with Component and EventEmitter from ‘@angular/core’.
That’s it. The error Cannot find name ‘Output’ should have disappeared.
Hope it helped 🙂
- Call ngOnInit() again from another function – Angular 9 ?
- ngOnChanges get new value and previous value – Angular
- Why do we need ngDoCheck lifecycle hook – Angular ?
- Global Angular CLI version is greater than your local version
- Upgrade Angular CLI to the latest version Angular 9 or 10 ?
- How to use new static option in ViewChild Angular 9 ?
- Project contents into angular components using ng-content
- Call ngOnInit() again from another function – Angular 9 ?
- ngAfterViewInit with Example – Angular
- ngAfterContentChecked with Example
- ngOnDestroy Example Angular
- Angular Component : In which lifecycle hook you can check value of DOM element ?
- @ContentChild TypeError: Cannot read property ‘nativeElement’ of undefined
- Access ng-content with @ContentChild – Angular Component
- How to select an element in a component template – Angular ?
- Difference between @ViewChild and @ContentChild – Angular Example
- Expected 0 type arguments, but got 1 : Angular
- Angular – Access template reference variables from component class ?
- Pass variable from parent to custom child component – Angular 9
- EventEmitter parameter value undefined for listener
- Programmatically navigate with React Router using history object
Thank u! U saved my life!! 🙂