@NgModule({
declarations: [components, directives, pipes],
imports: [modules],
providers: [service providers],
entryComponents: [components],
exports: [modules, components, directives, pipes],
bootstrap: [AppComponent]
})
takes a metadata object that describes how to
- compile a component's template
- create an injector at runtime
declarations
- declarables are components, directives and pipes
- declarables must belong to exactly one module
- these declared classes are visible only within the module
providers
- you list services here, they are available app-wide
- provider is an instruction to the DI system on how to obtain a value for a dependency
- most of the time, these dependencies are services that you create and provide
- when the Angular router lazy-loads a module, it creates a new injector (child)
bootstrap
- you can put more than one component
- these components are automaticaly added to entryComponents
entryComponents
- loads imperatively, (which means you’re not referencing it in the template), by type
- bootstrap and routed components are entryComponents automatically
- if a component isn't an entry component and isn't found in a template, the tree shaker will throw it away
A component can also be bootstrapped imperatively:
import { NgModule, ApplicationRef, DoBootstrap } from '@angular/core';
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
entryComponents: [ AppComponent ]
})
export class AppModule implements DoBootstrap {
ngDoBootstrap(appRef: ApplicationRef) {
appRef.bootstrap(AppComponent);
}
}
Feature modules
- Domain feature modules (dedicated to a particular application domain like editing a customer)
- Routed feature modules (whose top components are the targets of router navigation routes)
- Routing modules (defines routes, adds guard and resolver service providers to the module's providers)
- Service feature modules (they consist entirely of providers(like HttpClientModule). The root
AppModule
is the only module that should import service modules) - Widget feature modules (a widget module makes components, directives, and pipes available to external modules)