2017-08-19 72 views
0

我一直試圖解決我的問題無濟於事。無法綁定到DIRECTIVE,因爲它不是元素的已知屬性角AOT

的.html:

<li [myHighlight]="color" defaultColor="violet" routerLinkActive="active"><a [routerLink]="['user']">Users <span class="sr-only" >(current)</span></a></li> 

指令:

import { Directive, ElementRef, HostListener, Input } from '@angular/core'; 

@Directive({ 
    selector: '[myHighlight]' 
}) 
export class HighlightDirective { 

    constructor(private el: ElementRef) { } 

    @Input() defaultColor: string; 

    @Input('myHighlight') highlightColor: string; 

@HostListener('mouseenter') onMouseEnter() { 
    this.highlight(this.highlightColor || this.defaultColor || 'red'); 
} 

@HostListener('mouseleave') onMouseLeave() { 
    this.highlight(null); 
} 
    private highlight(color: string) { 
     this.el.nativeElement.style.backgroundColor = color; 
    } 
} 

app.module:

import { PermissionsDirective, HighlightDirective } from "./shared/directives/permisions.directive"; 

    @NgModule({ 
     imports: [ 
      .. 
     ], 
     declarations: [ 
      .. 
      HighlightDirective, 
      .. 
     ], 
     bootstrap: [ 
      ApplicationComponent, 
     ], 
     providers: [ 
      .. 
     ] 
    }) 
    export class AppModule { } 

錯誤,那是我得到:

Can't bind to 'myHighlight' since it isn't a known property of 'li'. 

我使用AOT編譯器使用這些設置:

{ 
    "compilerOptions": { 
     "target": "es5", 
     "module": "es2015", 
     "moduleResolution": "node", 
     "sourceMap": true, 
     "emitDecoratorMetadata": true, 
     "experimentalDecorators": true, 
     "removeComments": true, 
     "noImplicitAny": false, 
     "suppressImplicitAnyIndexErrors": true, 
     "skipLibCheck": true, 
     "lib": [ 
      "es2015", 
      "dom" 
     ] 
    }, 
    "files": [ 
      .. all the good stuff .. 
    ], 
    "angularCompilerOptions": { 
     "genDir": "aot", 
     "skipMetadataEmit": true 
    }, 
    "compileOnSave": true, 
    "buildOnSave": true 
} 

我的猜測是,因爲AOT編譯器,我應該定義不同於他們在導遊做指導。 This是我使用的指南。我也觀看了一個複數光學課程,但一切似乎都適合其他人。有人能指引我朝着正確的方向嗎?我不會在這裏,如果我沒有找到幾個小時...

編輯:它爲什麼認爲'myHighlight'是「li」的一個屬性,而'routerLinkActive'是(我希望)屬性的路由器指令?

+0

我只注意到從app.module生成的ngFactory不包含我的指令的導入。我會再調查一下。 –

回答

1

問題是,我在主application.module中聲明瞭這個指令,而不是那個正在使用它的指令。我仍然不明白爲什麼每個模塊都必須聲明它將使用的指令,而不是在主模塊中聲明它們一次。

相關問題