简体   繁体   English

如何在Angular 2中发出@Output事件

[英]How to emit a @Output event in Angular 2

Angular 2 version used: 2.0.0-alpha.44 使用Angular 2版本: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 我在这里关注了Angular文档.Below是我的组件的代码

@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) 但是,当我尝试分配EventEmitter(下面的行)时,我收到错误说“TypeError:router_1.EventEmitter不是函数”

@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. 但loadSummary似乎始终未定义。 How to emit the output signal ? 如何发出输出信号? Please help 请帮忙

As of beta.0 (and maybe earlier), EventEmitter is now in angular2/core . 从beta.0开始(可能更早), EventEmitter现在处于angular2/core

Emit an event using emit() : 使用emit()发出事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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