Angular 4-内存中模块的多个实例 [英] Angular 4 - Multiple instances of modules in memory

查看:63
本文介绍了Angular 4-内存中模块的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个大型的 Angular 4 应用程序,它使用 lazy loading preloadAllModules 的预加载策略.我们正在使用 Angular CLI .?

We have a large Angular 4 application using lazy loading along with the preloading strategy of preloadAllModules. We are using Angular CLI.

在Chrome检查器中查看内存问题时,我注意到我们到处都有多个模块实例.

While looking at a memory issue in the Chrome inspector, I noticed that we have multiple module instances everywhere.

如果我没看错,我们有83个 TranslateModule HttpModule 等实例...

If I am reading this right, we have 83 instances of TranslateModule and HttpModule etc...

我唯一能想到的是,每个 lazy loading 模块都有其自己的根,需要为该捆绑包创建自己的模块?

Only thing I can think of is that every lazy loaded module has its own root which needs to create its own module for that bundle?

是否有可能摆脱所有这些实例,或者仅仅是它是如何工作的?

Is it possible to get rid of all these instances or is that just how it works?

推荐答案

是否有可能摆脱所有这些实例?它有效吗?

Is it possible to get rid of all these instances or is that just how it works?

是的,只需将它们导入到根 AppModule 中即可.但是,虽然可以避免将 HttModule 放入每个已加载的模块中,并且只能将其一次导入到根 AppModule 中,但仍不能避免为 RouterModule 应该像 RouterModule.forChild(routes)这样使用.

Yes, just import them into root AppModule. However, while you can avoid putting HttModule into every loaded module and can import it only once into root AppModule, you can't avoid duplication for RouterModule which should be used like this RouterModule.forChild(routes).

之所以会发生复制,是因为每个延迟加载的模块都有自己的注入程序,并且具有所有导入模块的实例.因此,如果您有83个延迟加载的模块,并且每个模块导入 HttpModule RouterModule ,则每个导入的模块将有83个实例.

The duplication happens because every lazy loaded module has its own injector with its own instances of all imported modules. So if you have 83 lazy-loaded modules and each module imports HttpModule and RouterModule you will have 83 instances of each imported module.

有关更多信息,请点击此处:

For more information read here:

这篇关于Angular 4-内存中模块的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆