Sobes.tech
Middle+

What happens to a lazy module when leaving a route? What happens to components, directives, services?

sobes.tech AI

Answer from AI

When leaving a route associated with a lazy-loaded module in Angular, the module itself and its components are unloaded from memory if there are no more active references to them in the application. Components and directives declared in the lazy module are destroyed along with the module, as they only exist within the context of that module.

As for services, if a service is provided within the lazy module itself (via providers in the module decorator), then the services are also destroyed when the module is unloaded. However, if services are provided at the root level (providedIn: 'root'), they live in a singleton and are not destroyed when changing routes.

Thus, the lazy module and its components/directives are unloaded when leaving the route, but services are only unloaded if they are local to that module.

What happens to a lazy module when leaving a route… - sobes.tech