Angular course Β· Module 3: Modules

Feature Modules - Provinces of the Kingdom

2 min read Β·
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.ts

Spotted 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. 1. Which type of module contains reusable components, directives and pipes shared between feature modules?

  2. 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: [

Useful articles