Angular course Β· Module 3: Modules
Feature Modules - Provinces of the Kingdom
In this lesson2
Feature modules divide the application into logical parts - like provinces of feudal Japan, each with its own specialization.
Types of Modules
1. Feature Module (Province)
Groups business functionality:
// features/samurai/samurai.module.ts
@NgModule({
declarations: [
SamuraiListComponent,
SamuraiDetailComponent,
SamuraiFormComponent
],
imports: [
CommonModule,
SharedModule,
SamuraiRoutingModule
],
exports: [SamuraiListComponent]
})
export class SamuraiModule { }2. Shared Module (Common Resources)
Contains reusable elements:
// shared/shared.module.ts
@NgModule({
declarations: [
ButtonComponent,
CardComponent,
LoadingSpinnerComponent,
HighlightDirective,
TruncatePipe
],
imports: [CommonModule],
exports: [
// We export EVERYTHING that can be used in other modules
CommonModule,
ButtonComponent,
CardComponent,
LoadingSpinnerComponent,
HighlightDirective,
TruncatePipe
]
})
export class SharedModule { }3. Core Module (Command Center)
Singletons and elements loaded once:
// core/core.module.ts
@NgModule({
imports: [
CommonModule,
HttpClientModule
],
providers: [
AuthService,
LoggingService,
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
]
})
export class CoreModule {
// Protection against multiple imports
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error('CoreModule already loaded. Import only in AppModule!');
}
}
}Directory Structure
src/app/
βββ core/ # Singleton services, guards, interceptors
β βββ core.module.ts
β βββ services/
β β βββ auth.service.ts
β β βββ api.service.ts
β βββ guards/
β βββ interceptors/
β
βββ shared/ # Reusable components, directives, pipes
β βββ shared.module.ts
β βββ components/
β βββ directives/
β βββ pipes/
β
βββ features/ # Feature modules
β βββ samurai/
β β βββ samurai.module.ts
β β βββ samurai-routing.module.ts
β β βββ components/
β β βββ services/
β β βββ models/
β βββ dojo/
β
βββ app.module.ts
βββ app.component.ts
βββ app-routing.module.tsSpotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. Which type of module contains reusable components, directives and pipes shared between feature modules?
2. What does a Feature Module (province) group in Angular architecture?
These are 2 of 4 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Complete the SharedModule so that it makes ButtonComponent available to other modules, together with the module that provides the *ngIf and *ngFor directives and the async pipe.
- Horizontal ordering
Arrange the decorator tokens that protect CoreModule against multiple imports (constructor parameter):
- Vertical ordering
Order the directories of an Angular application according to convention (from the lowest layer to feature):
- Click in order
Click the elements in order to build the start of the providers property: providers: [