未捕获错误:无法编译HighlightDirective,因为它不是一个组件

时间:2016-10-03 21:15:24

标签: angular angular2-directives angular2-components

错误日志:

  

runtime_compiler.js:353未捕获错误:无法编译' HighlightDirective'因为它不是component.assertComponent
  ...

指令:

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


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

  constructor(private elementRef: ElementRef, private renderer :Renderer) {

  //this.elementRef.nativeElement.style.backgroundColor = 'green';
    this.renderer.setElementStyle(this.elementRef.nativeElement,'background-color', 'green');
  }

}

Html文件:

<h2> Custom Attribute Directive</h2>
<div dirHighlight> Some thing</div>

app.component.ts

import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';

@Component({
  selector: 'dir-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  entryComponents:[HighlightDirective],

})
export class AppComponent {

}

1 个答案:

答案 0 :(得分:2)

您需要在主模块中添加指令,而不是getHighestPrice() == 1

entryComponents

查看文档here

希望这会有所帮助!!