javascript - Declaration of directive or component in defferent modules, angular 4 -


is possable declare 1 directive/component many modules? can export one:

import { directive, elementref } '@angular/core';    @directive({ selector: '[appbigtext]' })  export class bigtextdirective {    constructor(el: elementref) {      el.nativeelement.style.fontsize = '100px'    }  }      module one:    import { bigtextdirective }   '../common/directives/dir225';  @ngmodule({    imports: [httpmodule...........        providers :[homeservice],    declarations: [      homecomponent,      detailcomponent,      bigtextdirective    ]    component:  import { bigtextdirective }   '../common/directives/dir225';    @component({    selector: 'my-app',    template: `  <router-outlet></router-outlet>    <div appbigtext>this text huge.</div>.    `  })    module 2 same inculding, have error 
error: type bigtextdirective part of declarations of 2 modules: homemodule , notesmodule! please consider moving bigtextdirective higher module imports homemodule , notesmodule. can create new ngmodule exports , includes bigtextdirective import ngmodule in homemodule , notesmodule.

in case need using same component/directive in multiple modules, means it's common. why don't follow error suggestion , extract bigtextdirective separate module , export it?

ngmodule({   // ...   providers: [     bigtexttexture   ]   exports: [     bigtexttexture   ] }) export class commonmodule {} 

so can re-use in high-level modules importing it:

@ngmodule({   imports: [     commonmodule   ],   declaration: [     ineedbigtexttexturecomponent   ] }) export class highlevelmodule {} 

let angular helps code organization!


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -