what is lazy loading in angular

what is lazy loading in angular

1 year ago 116
Nature

Lazy loading is a design pattern in Angular that loads NgModules as needed. It is the process of loading components, modules, or other assets of a website as they are required. Since Angular creates a Single Page Application (SPA), all of its components are loaded at once, which means that a lot of unnecessary libraries or modules might be loaded as well. This is where lazy loading comes in handy, as it allows Angular to load components and modules as and when they are needed.

Lazy loading is useful for large applications with lots of routes, as it helps keep initial bundle sizes smaller, which reduces load times. It is a technique in which the browser loads only the necessary components or modules according to the requirements, and all the modules will not be loaded regardless of the active route. Lazy loading can significantly improve the performance of the application, especially for enterprise-level applications.

To implement lazy loading in Angular, feature modules are used to arrange code, and a whole module is loaded only when needed, reducing the file size of the core bundles and maybe limiting access to bundles to just those who are permitted to use it. Lazy loading can be verified by running the application and noticing that the lazy loading module is loaded if and only if the route is hit.

In summary, lazy loading is a technique that can significantly improve the performance of an Angular application by loading only the necessary components or modules according to the requirements. It is useful for large applications with lots of routes and can be implemented using feature modules.

Read Entire Article