简体   繁体   English

放置在延迟加载模块的组件中时,不会初始化Angular 6指令

[英]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中使用您的directiveAppModule创建一个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.

相关问题 Angular 10 辅助路由器插座放在延迟加载模块时不工作 - Angular 10 auxiliary router outlet not working when placed in lazy loaded module 为什么Angular 6组件未在延迟加载的模块中路由 - Why Angular 6 component not routed in a lazy loaded module Angular 9 路由到延迟加载模块中的子组件 - Angular 9 routing to child component in lazy loaded module 急切加载模块的组件未显示在延迟加载模块中 - eagerly loaded module's component is not being displayed in lazy loaded module 角度2:如何从组件内部读取延迟加载的模块的路由 - Angular 2: How to read a lazy-loaded Module's routes from within a Component Angular:无法在延迟加载的模块组件中定义路由器出口: - Angular: Cant define router outlet in lazy loaded module component: Angular-在路由到延迟加载的模块时将数据传递到主机组件 - Angular - pass data to host component upon routing to lazy loaded module 角度2/4:与延迟加载的模块及其组件共享一个组件 - Angular 2/4: Share a component with lazy loaded module and its components 在 Angular 的延迟加载模块中显示组件时出错 - Getting error while displaying a component in lazy loaded module in Angular 延迟加载模块中的角度延迟加载模块 - angular lazy load module in lazy loaded module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM