使用的Angular 2版本:2.0.0-alpha.44
我正在尝试从组件发出输出信号。我关注了Angular docs here .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");
}
}
但是我得到的错误是" TypeError:router_1.EventEmitter不是函数"当我尝试分配EventEmitter(在线下)
@Output() loadSummary: EventEmitter = new EventEmitter();
我检查了link并将该行修改为
@Output() loadSummary: EventEmitter;
但是loadSummary似乎始终未定义。 如何发出输出信号?请帮助
答案 0 :(得分:4)
从beta.0开始(可能更早),EventEmitter
现在位于angular2/core
。
使用emit()
:
@Output() loadSummary: EventEmitter<any> = new EventEmitter();
...
this.loadSummary.emit("event");