Does the Angular 2+ Router Unload the Previous Lazy Loaded Module

故事扮演 提交于 2019-12-10 10:23:37

问题


I have been working on numerous ngModules, each encapsulating their own set of components, services, directives, etc. They are large. Now that I am ready to string my app up and lazily route each module onto the main router-outlet, I am concerned about overall memory consumption, particularly for low-end devices (tablets on up only, not mobile). So my question is when the Router lazy loads a new module, is the previous lazy loaded module unloaded? I can only find information about JIT, AOT, pre-loading and lazy loading but there is a dearth of resources or discussions about Angular 2+ and memory management in a post-loaded context.

Thank you in advance for considering my question.


回答1:


I can confirm that though they aren't initially loaded using the lazy loading method they are not unloaded after routing to another module. whats worse is unloading modules is not going to be supported by ngx. here is the tweet where this exact question was asked of Brad Green.

https://twitter.com/bunsofaluminum/status/642397294474756096 .

I wanted to add a quick comment about unloading components. you can unload components and the data associated with them there are two ways I know of DynamicComponentLoader which is part of core and OnDestroy which implements just like OnInit . You should be able to leverage both of those to help with overall memory load especially if your using alot of services which you can unsubscribe to OnDestroy.




回答2:


Looks like "not implemented yet" is still the correct answer as of 2017-04.

However, the developer Sander Elias proposes a workaround:

The only way to flush memory is a page-reload. The easiest way to do that is using a href instead of a routerlink to navigate between certain parts of your application.



来源:https://stackoverflow.com/questions/43462243/does-the-angular-2-router-unload-the-previous-lazy-loaded-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!