Angular Modules and Lazy Loading
This is the most basic Angular Module:
@NgModule decorator defines the module. The metadata defined does all the work. Any components imported and defined in the meta down now live in the module. Here you can see how components are imported and then defined in declarations . Other modules are defined in imports . Services used to be injected into components are defined in providers .
The last two properties on the ngModule object are for entry-components (entryComponents and boostrap). Entry-components are components that are loaded by the Angular’s render engine even though they are not referenced by templates in components. The boostrap component is simple the main component in the application for that module. Entry components are components that are not referenced in other component templates but in are referenced functionally. The most common example is a component like a model that has a show or open function to display the component.
All of the core Angular code is broken up in to modules just like this. Modules are the center of Angular. Modules drive Angular’s dependency injection engine. Depending on where services is imported into which module, services will be a singleton or not (quick tip on singleton service: if the whole app needs a service, import the service in the main App module).
Every app will have an App level module. This is the main module. But the Angular docs recommend breaking out components in cohesive sections of the app into their own module. This is called a feature module by the Angular docs. Feature modules get imported into the App module and referenced in the imports array.
Here is my post on singleton services. Service life cycle is controlled by the module. When a module is active through the router, the module’s injector is created with all the associated services. Since modules form a tree structure with the App module at the top, services in the top are available all the way down. The App module injector is never destroyed and all the services will be the same instance. This is affected as you move down the tree into feature modules. Go read the docs for more detail.
Get Wes’s stories in your inbox
Join Medium for free to get updates from this writer.
Outside of a feature module or App module, it is common to see a shared module. This is a module style where you could wrap all your common components that are reused throughout your app. Its like your own component library. This is really just a tool to organize your code.
One of the most import features of modules is the ability to lazy load feature modules. This means that you do not have to bring down all your app code at once. You can bring down the bare bones and based on the active route bring down what you will need next. This a basic strategy to improve app performance and decrease and distribute network load. To do this, an app-routing.module.ts file and a routing file for each feature module is needed. For more info, instructions for using Angular CLI to setup lazy loading.
Add the route module to the feature module imports (line 9 below).
Add the feature route module file.
Now, lazy loading is setup. When your route to the new module a new chunk will show up in the Network tab.
That wraps up a lot of what you can do with NgModule.
If you found this helpful at all please hit the clap button about 50 times. It helps this post reach more people.
I post on twitter about new articles here. I would also really enjoy hearing any response. Just leave them below!










