我有一个回调,我想由多个事件触发。我试过了
userIsActive(){
//do something
}
@HostListener('document:mousemove') this.userIsActive;
@HostListener('document:keypress') this.userIsActive;
@...
但它不起作用,duplicate identifier 'userIsActive'
。我知道我可以将新函数定义为回调,
@HostListener('document:mousemove') foo1(){ userIsActive(); };
@HostListener('document:keypress') foo2(){ userIsActive(); };
@...
但创建2个具有任意数字的新函数似乎是不必要的,也不太可读。有没有办法将对现有回调的引用传递给@HostListener
?
答案 0 :(得分:2)
无需定义多个方法,您可以多次装饰相同的方法。
例如:
@HostListener('window:keyup')
@HostListener('window:keydown')
handle() {
console.log('happened')
}