Module and Feature Modules

( 0 users )

Angular modules are designed to distribute our application into blocks of different modules which are more manageable. As our application starts growing incorporating more features and functionalities, it becomes tedious to manages if we keep on adding code into our single block of application. Then there comes a time when we have to code our new big features into a different module. We then use that module in our main application module.

Angular modules are independent block of distributable code. These can be exported as a library. For e.g. Angular provides us with BrowserModule, HttpModule, FormsModule etc. Similarly we can make our own Angular module which can be used in one or more application depending on the scope of functionality which we are building,

It is much easier to do with Angular Modules.

An Angular Module is a class with ngModule decorator.

Features of and Angular module

It declares its own set of Components, Directives and Pipes.

It can be exported as an angular module where other modules con consume its components, directives and pipes.

It provide services to itself and other modules.

It can import other modules in its own module declaration and use their components, directives and pipes.

If we look at our default app.module.ts file, we will find something similar code like this:

The @ngModule() directive have some different sets of settings. Let's look at them one by one.

imports

This is an array where we define other modules that we want to import in our main application module such as BrowserModule, FormsModule or any other third party module. One thing to note that when we export our module, all other modules that we imported are also available in our exported module. If A import B and C imports A, this means C does not have access to B.

declaration

This is an array which defines all the components, directives, pipes that belong to our angular module. Each of them must belong to only one angular module.

bootstrap

This is used to load the application module in Angular ecosystem. The application must bootstrap at least one component which is the root application component. It should only be used in the root application module.

exports

This is an array that is used to export components, directives and pipes to other module.

providers

This is an array where we define services of our module. This can be either at root level or any component level. If we have defined our service in providers array at root level, then that service will also be available to all child components. If we want a service available to a particular component only then we define that service to that component's provider array only.

Feature Module

A Feature Module in Angular is a module other than our main application root module that is decorated by @ngModule() decorator and hence share the same property configurations like application root module. You can also say it a custom module or an external module, but Feature Module is an Angular way of saying wherein we separate our features of our main application into a different module.

As our application starts growing bigger, it's better to re-factor at early stage so that the application stays manageable. Implementing Feature modules is a way of re-factoring our application.

Example application - Mail Client

Ok, so now we will move on to building an example application which is Mail Client. We will introduce two Feature modules - Mail Box and Address Book in our main application root module i.e. Mail Client.

MailClient component

Here we are just showing the version of the mail client which we input from main application component.

Note : Both in the MailBoxModule and AddressBookModule, we are not importing BrowserModule but we are importing CommonModule. The reason is that some essential services to launch and run a browser app is provided by BrowserModule. BrowserModule also re-exports CommonModule from @angular/common. We should not import BrowserModule in any other module. Feature modules and lazy-loaded modules should import CommonModule instead.