本文介绍了如何使用HostListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用@HostListener.我发现的唯一信息是在备忘单:

I am wondering how can I use @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(…)

有什么想法吗?

推荐答案

该错误消息非常准确.装饰器仅用于装饰函数(或类,字段,参数等),只需将其放在要装饰的代码之前:

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)
}

这篇关于如何使用HostListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 00:03