简体   繁体   中英

Angular 6 directive is not initialized when placed in lazy loaded module's component

Very new to Angular 6.

I have declared a directive in AppModule and in the appComponent template if I apply that directive it works, In the app component there is a router outlet in which a lazy loaded module gets loaded at very beginning.

Now if I want to apply the directive in any component template of the lazyloaded module, the directive is not being initialized. The directive is an attribute directive and I have declared it properly and used too because inside app component it's working fine.

Not getting any clue, please help.

I see there is an issue with your design. Instead of having your directive in the AppModule , create a SharedModule and then implement the directive in there. Import SharedModule everywhere else in your app. This way you can access your directive from external components (from other modules).

Make sure you declare and export it inside SharedModule

I thought if anything declared in app module should be available to all modules

No this is not correct, Child modules does not know what you have inside the AppModule . ChildModule 's are just another independent modules like AppModule . You could have simply exposed AppModule to ChildModule , but that might cause the circular dependency. That is the reason you have to make use of SharedModule

If we create directives in the feature module/shared module make sure that we need to have them put in the declaration section and export section. import this feature/shared module where it is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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