[英]Angular 6 directive is not initialized when placed in lazy loaded module's component
Very new to Angular 6. 对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. 我已经在AppModule和appComponent模板中声明了一个指令,如果我应用它的指令就可以了。在app组件中有一个路由器插座,其中一个延迟加载的模块在一开始就被加载。
Now if I want to apply the directive in any component template of the lazyloaded module, the directive is not being initialized. 现在,如果我想在lazyloaded模块的任何组件模板中应用该指令,那么该指令不会被初始化。 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. 不要在AppModule
中使用您的directive
, AppModule
创建一个SharedModule
,然后在那里实现该directive
。 Import SharedModule
everywhere else in your app. 在应用中的其他位置导入SharedModule
。 This way you can access your directive
from external components (from other modules). 这样,您可以从外部组件(从其他模块)访问您的directive
。
Make sure you declare and export it inside SharedModule
确保在SharedModule
声明并导出它
I thought if anything declared in app module should be available to all modules 我想如果在app模块中声明的任何内容都应该可用于所有模块
No this is not correct, Child modules does not know what you have inside the AppModule
. 不,这不正确,子模块不知道你在AppModule
里面有AppModule
。 ChildModule
's are just another independent modules like AppModule
. ChildModule
只是另一个独立的模块,如AppModule
。 You could have simply exposed AppModule
to ChildModule
, but that might cause the circular dependency. 您可以简单地将AppModule
暴露给ChildModule
,但这可能会导致循环依赖。 That is the reason you have to make use of SharedModule
这就是你必须使用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. 导入此功能/共享模块使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.