Is there a way to lazy load components, not modules, in Angular?

核能气质少年 提交于 2019-12-04 00:12:41

Lazy loading a component is not possible. Reason for this behavior is the structure of angular. Components in angular have to be part of a module. Angular module is a container where you define all the components, services, pipes e.t.c. present inside. While building the application dist, all the dependencies declared in the module are included to make a chunk (transpiled js code). All the directly imported modules together form the main chunk whereas modules marked as lazily loaded form a separate chunk that sits on the server untill the respective route is hit and a call for it is made from the client. Now components do not form a chunk and hence it is not possible

Currently (Angular 8 timeframe) there are two 3rd party libraries that make lazy-loading of components easier:

Note: both of these libraries are built upon NgModuleFactoryLoader, which is deprecated in Angular 8, but there is no alternative yet ... 


The Angular team has announced that lazy loading of components should become easier with Ivy (Angular 9?), but it is not clear yet how this will look like...

A bit hacky ... but possible (future versions of Angular (beyond ver. 7) will implement an easier apporach)

https://blog.angularindepth.com/dynamically-loading-components-with-angular-cli-92a3c69bcd28

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