繁体   English   中英

指令抛出错误,无法理解

[英]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突出显示: onMouseEnterthis.highlight('yellow'); 有什么问题?

角度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文档中找到更多示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM