简体   繁体   English

如何使用HostListener

[英]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.

相关问题 如何在角度主机侦听器中使用键盘组合键 - How to use keyboard keys combination in angular hostlistener 是否可以在服务中使用 HostListener? 或者如何在 Angular 服务中使用 DOM 事件? - Is it possible to use HostListener in a Service? Or how to use DOM events in an Angular service? 如何使用 @HostListener('window:beforeunload') 取消路由? - How can I use @HostListener('window:beforeunload') to cancel routing? 如何使用@HostListener('window:beforeunload')调用方法? - How can I use @HostListener('window:beforeunload') to call a method? location.hash更改时如何使用@HostListener? (角度2) - How to use @HostListener when location.hash changed? (angular 2) 如何正确使用HostListener装饰器来调用超类函数? - How to use the HostListener decorator correctly to call a superclass function? 如何使用 Angular @HostListener onLoad 获取当前视口的宽度? - How to use the Angular @HostListener onLoad to get the current width of the viewport? Angular 如何测试@HostListener - Angular How to test @HostListener 如何在Angular2中销毁HostListener - How to destroy HostListener in Angular2 如何使 HostListener 滚动工作? - How to make HostListener for scroll work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM