How to emit @Output event in Angular 2

Angular 2 version used: 2.0.0-alpha.44

I am trying to emit an output from a component. I followed Angular docs here .Below is the code of my component

@Component({ selector: 'summary' }) @View({ directives: [NgFor, NgIf, ROUTER_DIRECTIVES, LogoutComponent, BarChartDirective, SunburstChartDirective], templateUrl: 'view/summary-component.html' }) export class SummaryComponent { @Output() loadSummary: EventEmitter = new EventEmitter(); project: Project; data: any; constructor(public http: Http, public router: Router, public xxxGlobalService: XXXGlobalService) { console.log("SummaryComponent: Created"); this.getSummary() } getSummary() { this.project = this.xxxGlobalService.getOpenedProject(); var url = "/project_summary?username="+ this.xxxGlobalService.getUser().name + "&project_id=" + this.project.id; this.http.get(url) .map(res => res.json()) .subscribe( data => this.extractData(data), err => this.logError(err), () => console.log('SummaryComponent: Successfully got the summary') ); } extractData(data) { this.data = data; console.log("SummaryComponent: Emitting loadSummary event"); this.loadSummary.next("event"); } } 

However, I get the error "TypeError: router_1.EventEmitter is not a function" when I try to allocate an EventEmitter (below the line)

 @Output() loadSummary: EventEmitter = new EventEmitter(); 

I checked the link and changed the line so that it looked like

 @Output() loadSummary: EventEmitter; 

But loadSummary is apparently undefined. How to emit an output signal? Please, help

+5
source share
1 answer

Starting in beta (and possibly earlier), EventEmitter now in angular2/core .

Output an event using emit() :

 @Output() loadSummary: EventEmitter<any> = new EventEmitter(); ... this.loadSummary.emit("event"); 
+4
source

Source: https://habr.com/ru/post/1236332/


All Articles