[英]Angular 2 @Output does nothing
I'm trying to build a component that emits custom events. 我正在尝试构建一个发出自定义事件的组件。 I'm not sure why the events are not triggered.
我不确定为什么未触发事件。 If I look on browser developer tools, I can see that the events are attached to element.
如果使用浏览器开发人员工具,则可以看到事件已附加到element上。
I even tried to create the small example that is provided by the angular documentation: https://angular.io/docs/ts/latest/api/core/Output-var.html but with no luck. 我什至尝试创建角度文档提供的小示例: https : //angular.io/docs/ts/latest/api/core/Output-var.html但没有运气。
Here is the small code: 这是小代码:
import {Component} from "angular2/core";
import {Directive} from "angular2/core";
import {Output} from "angular2/core";
import {EventEmitter} from "angular2/core";
import {bootstrap} from "angular2/platform/browser";
@Directive({
selector: 'interval-dir',
})
class IntervalDir {
@Output() everySecond = new EventEmitter();
@Output('everyFiveSeconds') five5Secs = new EventEmitter();
constructor() {
setInterval(() => this.everySecond.emit("event"), 1000);
setInterval(() => this.five5Secs.emit("event"), 5000);
}
}
@Component({
selector: 'app',
template: `
<interval-dir (every-second)="everySecond()" (every-five-seconds)="everyFiveSeconds()">
</interval-dir>
`,
directives: [IntervalDir]
})
class App {
everySecond() { console.log('second'); }
everyFiveSeconds() { console.log('five seconds'); }
}
bootstrap(App);
No error is thrown , no console.log either. 没有引发错误,也没有console.log。
Note: I'm using angular 2 - beta0 注意:我正在使用角度2-beta0
Am I missing something ? 我想念什么吗? Thanks
谢谢
Don't use kebab-case for events and properties anymore. 不要再将kebab-case用于事件和属性了。 Use camelCase :
使用camelCase :
@Component({
selector: 'app',
template: `
<interval-dir
(everySecond)="everySecond()"
(everyFiveSeconds)="everyFiveSeconds()"
></interval-dir>
`,
directives: [IntervalDir]
})
class App { /* ... */ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.