关于 Angular2 指令,我想使用outputs
而不是使用@Output
因为我有很多自定义事件并且希望保持 DRY。
但是,我有TypeError: Cannot read property 'subscribe' of undefined
,而且我不知道为什么会发生这种情况。
http://plnkr.co/edit/SFL9fo?p=preview http://plnkr.co/edit/SFL9fo?p=preview
import { Directive } from "@angular/core";
@Directive({
selector: '[my-directive]',
outputs: ['myEvent']
})
export class MyDirective {
constructor() {
console.log('>>>>>>>>> this.myEvent', this.myEvent);
}
}
这是使用该指令的应用程序组件
您需要初始化输出:
import { Directive } from "@angular/core";
@Directive({
selector: '[my-directive]',
outputs: ['myEvent']
})
export class MyDirective {
myEvent:EventEmitter<any> = new EventEmitter(); // <-----
constructor() {
console.log('>>>>>>>>> this.myEvent', this.myEvent);
}
}
您还可以使用@HostListener
装饰器:
@Directive({
selector: '[my-directive]'
})
export class MyDirective {
@HostListener('myEvent')
myEvent:EventEmitter<any> = new EventEmitter(); // <-----
constructor() {
console.log('>>>>>>>>> this.myEvent', this.myEvent);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)