[英]How to use HostListener
I am wondering how can I use @HostListener
. 我想知道如何使用@HostListener
。 The only info I've found, was in the cheatsheet : 我发现的唯一信息是在备忘单中 :
@HostListener('click', ['$event']) onClick(e) {...}
I am trying to log the element that user has clicked on. 我正在尝试记录用户点击的元素。 I've tried like that: 我试过这样的:
@HostListener('click')({
onClick(e) {
console.log(e)
}
})
But then I receive an error of: 但后来我收到一个错误:
TypeError: decorator is not a function(…)
Any ideas? 有任何想法吗?
The error message is quite accurate. 错误消息非常准确。 A decorator is just for decorating a function (or a class, field, parameter, ...) Just put it before the code you want to decorate: 装饰器只是用于装饰函数(或类,字段,参数......)只需将它放在要装饰的代码之前:
@HostListener('click', ['$event'])
onClick(e) {
console.log(e)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.