An error occurred: @Output not initialized
AngularClickAngular Event-EmitterAngular Problem Overview
I'm working on an angular app for managers to keep track of their teams, and I'm stuck with an @Output error :
An error occurred: @Output deleteMeeting not initialized in 'MeetingItemComponent'.
I have a Meetings component, generating a list of MeetingItem components. I want to perform actions when the user clicks on different buttons (edit, delete, show details).
Here is my parent Meetings template :
<div class="meeting__list" [@newMeeting]="meetings.length">
<app-meeting-item
*ngFor="let meeting of meetings"
[meeting]="meeting"
(deleteMeeting)="deleteMeeting($event)"
(openMeetingDialog)="openMeetingDialog($event)"
(messageClick)="openMessage($event)"
></app-meeting-item>
</div>
My MeetingItem template (only the part concerned by this post) :
<span class="meeting__actions">
<mat-icon *ngIf="meeting.message" (click)="onMessageClick(meeting)" matTooltip="Read the message"
matTooltipPosition="above" class="icon--notes">notes</mat-icon>
<mat-icon (click)="onOpenMeetingDialog(meeting)" matTooltip="Edit this meeting" matTooltipPosition="above" class="icon--edit">edit</mat-icon>
<mat-icon (click)="onDeleteMeeting(meeting.id)" matTooltip="Delete this meeting" matTooltipPosition="above" class="icon--delete">delete_outline</mat-icon>
</span>
My MeetingItem component :
import { Component, Input, Output } from '@angular/core';
import { EventEmitter } from 'events';
@Component({
selector: 'app-meeting-item',
templateUrl: './meeting-item.component.html',
styleUrls: ['./meeting-item.component.scss']
})
export class MeetingItemComponent {
@Input() meeting;
@Output() deleteMeeting = new EventEmitter();
@Output() openMeetingDialog = new EventEmitter();
@Output() messageClick = new EventEmitter();
constructor() {}
onDeleteMeeting(meetingId) {
this.deleteMeeting.emit(meetingId);
}
onOpenMeetingDialog(meeting) {
this.openMeetingDialog.emit(meeting);
}
onMessageClick(meeting) {
this.messageClick.emit(meeting);
}
}
Angular Solutions
Solution 1 - Angular
To make your code work in a stackblitz, I had to replace
import { EventEmitter } from 'events';
with
import { EventEmitter } from '@angular/core';
Solution 2 - Angular
Had the same error,
Import was correct like
import { EventEmitter } from '@angular/core';
But the variable definition was wrong:
@Output() onFormChange: EventEmitter<any>;
Should be:
@Output() onFormChange: EventEmitter<any> = new EventEmitter();
Solution 3 - Angular
I had also the same error. I found that Auto Import extension of VS Code did this. import * as EventEmitter from 'events';
is imported instead of import { EventEmitter } from '@angular/core';
So make sure if the import is ok or not.
Solution 4 - Angular
In your component just use core angular module. Simply put this code at beginning of file.
import { EventEmitter } from '@angular/core';
Solution 5 - Angular
I had the same problem even importing from @angular/core
.
The problem: I was initializing the EventEmmitter
object in the ngOnInit
method from my component class.
Solution: I moved the initialization to the component's class constructor.
Solution 6 - Angular
change the import : import { EventEmitter } from 'events';
with : import { EventEmitter } from '@angular/core';
Solution 7 - Angular
For me it works if I change below imports import { EventEmitter } from 'events';
to
import { Component, Output ,EventEmitter} from '@angular/core';
Solution 8 - Angular
@Output() isAbout: EventEmitter<boolean> = new EventEmitter();
This should be the whole syntax to make it work you need the instance of the event emitter