简体   繁体   中英

How to emit a @Output event in Angular 2

Angular 2 version used: 2.0.0-alpha.44

I am trying to emit a Output signal from a component. I followed the 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 error saying "TypeError: router_1.EventEmitter is not a function" when i try to allocate the EventEmitter (below line)

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

I checked the link and modified the line to look like

@Output() loadSummary: EventEmitter;

But the loadSummary seems to be undefined throughout. How to emit the output signal ? Please help

As of beta.0 (and maybe earlier), EventEmitter is now in angular2/core .

Emit an event using emit() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM