Root Module Example:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser'; // in root modules
@NgModule({
imports: [ BrowserModule ], // modules to import
providers: [ ... ], // services; CAUTION: use 'providers' only in root module
declarations: [ ... ], // components, directives, pipes that this module contains
bootstrap: [ AppComponent ] // only in root module
})
export class AppModule { }
Feature Module Example:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';; // in feature modules
@NgModule({
imports: [ CommonModule ], // modules to import
providers: [ ... ], // services
declarations: [ ... ], // directives that this module contains; these are private/module-scoped
exports: [ ... ], // directives to export with the module (non-private directives)
})
export class AppModule { }
2 types of modules:
- Root modules
- Feature modules
- always called
AppModule
- imports
BrowserModule
(for web projects), which includes theCommonModule
already (which is imported into feature modules) - 1 per project
- important for bootstrap process
- bootstrap happens in
main.ts
:
- bootstrap happens in
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule); // alternatively, the compilation can be part of the build flow (AOT)
- : 0-n possible per project
- imports
CommonModule
(instead ofBrowserModule
) - the
exports
keyword can be used to export directives (etc.) - types
- lazy modules := loaded on demand, when user navigates to their routes
- eager modules := loaded when in root module
https://angular-2-training-book.rangle.io/handout/modules/feature-modules.html