将自定义Direcitves添加到自定义库

时间:2019-04-02 04:53:39

标签: angular typescript

我想创建一个自定义库。应该导入自定义指令的地方!!一切都很好!自定义指令未实现

@Directive({

    selector:'[demo-dir]',
})
export class DemoDirective{

constructor(private elref:ElementRef,private Renderer:Renderer2){

}
@HostListener('click') mouseover()
{

    alert("test")
    console.log("test")
}
ngOnInit()
{
this.Renderer.setStyle(this.elref.nativeElement,'color','red');
}

自定义库模块:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DemoComponent } from './demo.component';
import { DemoDirective } from './demo.directive';

@NgModule({

  declarations: [DemoComponent,DemoDirective],
  imports: [
    CommonModule
  ],
  exports:[DemoComponent]
})
export class DemoModule { }

并且我已经将我的自定义库模块导入了app.module.ts

1 个答案:

答案 0 :(得分:0)

您编写的指令很好,您没有从CF模块中导出DemoDirective指令:

DemoModule

不导出指令,即使导入@NgModule({ declarations: [DemoComponent, DemoDirective], imports: [ CommonModule ], exports:[DemoComponent, DemoDirective] }) export class DemoModule { } ,它也不会在其他组件中可见。