简体   繁体   English

不属于路由的延迟加载模块

[英]Lazy Load Modules which are not part of routes

How can I load modules/components with lazy load those are being used in another component/module? 如何通过延迟加载来加载正在其他组件/模块中使用的模块/组件?

Let's say there are below 3 modules, where login and home are being loaded with lazy load. 假设下面有3个模块,其中的懒惰加载(login and home)被加载。

  • AppModule (main) AppModule(主)
  • LoginModule 登录模块
  • HomeModule 家庭模块

Now, I've created another module lib module which contains few custom components like grid, buttons etc.. these components will be consumed by components of a home or other modules and will never going to be used directly from any routes. 现在,我创建了另一个模块lib模块,其中包含一些自定义组件,例如网格,按钮等。这些组件将由home或其他模块的组件使用,并且永远不会在任何路由中直接使用。

Here, if lib module is imported into a home and other modules it's being duplicated in each chunk file. 在这里,如果将lib模块导入到home和其他模块中,则它将在每个块文件中复制。

Is there any way to lazy load lib module along with routes without duplicating code? 有什么办法可以在不复制代码的情况下将lib模块和路由一起延迟加载?

I also tried to import lib into app module but it's not working with a lazy load of the home module and gives an error that If 'grid' is an Angular component, then verify that it is part of this module. 我也尝试将lib导入应用程序模块,但是它不能与home模块的延迟加载一起工作,并给出一个错误,如果'grid'是Angular组件,则请验证它是否是此模块的一部分。

Since this module is not part of any routing , ti seems to me kind of shared module . 由于此模块不是任何路由的一部分,因此在我看来ti是一种共享模块。 And your shared module should be imported only once in the whole application. 并且您的共享模块应该在整个应用程序中仅导入一次。

You should import the core module into main module with decorator as @optional and @SkiptoSelf which will ensure to load the module only once in the whole application. 您应该将核心模块导入到带有装饰器的主模块中,装饰器为@optional和@SkiptoSelf,这将确保在整个应用程序中仅加载一次模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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