指令抛出错误,无法理解
Directive throw error, not able to understand
我使用 angular5,在应用程序中我创建了一个抛出错误的指令。我根本无法理解这个问题有人在这里帮助我吗?
代码:
import { Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core';
@Directive({
selector: '[zoom]'
})
export class ZoomDirective {
constructor(private el:ElementRef, private renderer:Renderer) {
@HostListener('mouseenter') onMouseEnter() {
this.highlight('yellow');
}
}
}
错误:
cannot find onMouseEnter, did you mean onmouseenter
我在 onMouseEnter
和 this.highlight('yellow');
处被突出显示,这是什么问题?
angular cli 详细信息:
Angular CLI: 1.6.5
Node: 6.11.4
OS: win32 x64
Angular: 5.2.3
@HostListener('mouseenter')
应该在构造函数之外声明:
export class ZoomDirective {
constructor(private el:ElementRef, private renderer:Renderer) { }
@HostListener('mouseenter') onMouseEnter() {
this.highlight('yellow');
}
private highlight(color: string) {
this.el.nativeElement.style.backgroundColor = color;
}
}
可以在 Angular documentation 中找到更多示例。
我使用 angular5,在应用程序中我创建了一个抛出错误的指令。我根本无法理解这个问题有人在这里帮助我吗?
代码:
import { Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core';
@Directive({
selector: '[zoom]'
})
export class ZoomDirective {
constructor(private el:ElementRef, private renderer:Renderer) {
@HostListener('mouseenter') onMouseEnter() {
this.highlight('yellow');
}
}
}
错误:
cannot find onMouseEnter, did you mean onmouseenter
我在 onMouseEnter
和 this.highlight('yellow');
处被突出显示,这是什么问题?
angular cli 详细信息:
Angular CLI: 1.6.5
Node: 6.11.4
OS: win32 x64
Angular: 5.2.3
@HostListener('mouseenter')
应该在构造函数之外声明:
export class ZoomDirective {
constructor(private el:ElementRef, private renderer:Renderer) { }
@HostListener('mouseenter') onMouseEnter() {
this.highlight('yellow');
}
private highlight(color: string) {
this.el.nativeElement.style.backgroundColor = color;
}
}
可以在 Angular documentation 中找到更多示例。