简体   繁体   English

动态加载模块组件A​​ngular2

[英]Load module components dynamically Angular2

Basically there is a module.ts in every created module of the app for example: 基本上,在应用程序的每个已创建模块中都有一个module.ts,例如:

ng generate module newModuleName

then the module is created with the newModuleName.module.ts file 然后使用newModuleName.module.ts文件创建模块

In the file, there is @NgModule with a declarations element. 在文件中,有@NgModule及其declarations元素。

So I want to dynamically modify the declarations array to prevent all components from being loaded in memory at the same time. 因此,我想动态地修改声明数组,以防止所有组件同时加载到内存中。

How can I do that? 我怎样才能做到这一点?

If your goal is to prevent all of the components from being downloaded at one time on startup, you can use lazy loading. 如果您的目标是阻止所有组件在启动时一次下载,则可以使用延迟加载。

Regarding memory, when a component is activated by routing to it or showing it as a child component, it is loaded in memory. 关于内存,当通过路由到某个组件或将其显示为子组件来激活该组件时,该组件将被加载到内存中。 As soon as the user navigates away from the page showing the component, the component is destroyed. 一旦用户离开显示该组件的页面,该组件就会被破坏。

So you don't have to do anything in the declarations to make this happen. 因此,您无需在declarations中进行任何操作即可实现此目的。

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

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