Shared modules help you keep common parts of your app in one place. This makes your code cleaner and easier to manage.
Shared modules for reusable components in Angular
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { SomeComponent } from './some.component'; @NgModule({ declarations: [SomeComponent], imports: [CommonModule], exports: [SomeComponent] }) export class SharedModule {}
Use declarations to list components, directives, and pipes in this module.
Use exports to make these components available to other modules.
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { ButtonComponent } from './button.component'; @NgModule({ declarations: [ButtonComponent], imports: [CommonModule], exports: [ButtonComponent] }) export class SharedModule {}
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { HighlightDirective } from './highlight.directive'; @NgModule({ declarations: [HighlightDirective], imports: [CommonModule], exports: [HighlightDirective] }) export class SharedModule {}
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { DateFormatPipe } from './date-format.pipe'; @NgModule({ declarations: [DateFormatPipe], imports: [CommonModule], exports: [DateFormatPipe] }) export class SharedModule {}
This example shows a shared module with a button component. The main app uses the shared button by importing SharedModule. The button appears inside the app's main page.
import { Component, NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-button', template: `<button>Click me!</button>` }) export class ButtonComponent {} @NgModule({ declarations: [ButtonComponent], imports: [CommonModule], exports: [ButtonComponent] }) export class SharedModule {} @Component({ selector: 'app-root', template: `<h1>Welcome</h1><app-button></app-button>` }) export class AppComponent {} @NgModule({ declarations: [AppComponent], imports: [BrowserModule, SharedModule], bootstrap: [AppComponent] }) export class AppModule {}
Always export components, directives, or pipes you want to reuse outside the shared module.
Import CommonModule in shared modules to use common Angular directives like ngIf and ngFor.
Do not provide services in shared modules to avoid multiple instances; use core modules for services.
Shared modules group reusable components, directives, and pipes.
They help keep your app organized and avoid repeating code.
Import shared modules in feature modules to use their exported parts.
Practice
Solution
Step 1: Understand the role of shared modules
Shared modules are designed to hold components, directives, and pipes that multiple parts of the app can use.Step 2: Compare with other module purposes
Root modules handle app startup, routing modules manage navigation, and environment files store settings, not reusable UI parts.Final Answer:
To group reusable components, directives, and pipes for easy sharing -> Option CQuick Check:
Shared module = reusable parts [OK]
- Confusing shared modules with root or routing modules
- Thinking shared modules store app settings
- Assuming shared modules handle navigation
Solution
Step 1: Understand Angular module metadata
Components must be declared in declarations to be recognized, and exported in exports to be usable outside the module.Step 2: Check other arrays usage
Imports bring in other modules, providers register services, bootstrap is for root components only.Final Answer:
Add the component to both declarations and exports arrays in @NgModule -> Option AQuick Check:
Declare + export = share component [OK]
- Putting components only in imports or providers
- Forgetting to export after declaring
- Using bootstrap array in shared modules
FeatureComponent uses SharedButtonComponent?
@NgModule({
declarations: [SharedButtonComponent],
exports: [SharedButtonComponent]
})
export class SharedModule {}
@NgModule({
imports: [SharedModule],
declarations: [FeatureComponent]
})
export class FeatureModule {}
If FeatureComponent template contains <app-shared-button>Click</app-shared-button>, what happens?Solution
Step 1: Check module imports and exports
SharedButtonComponent is declared and exported in SharedModule, which is imported by FeatureModule.Step 2: Confirm component usage in FeatureComponent
Since FeatureModule imports SharedModule, FeatureComponent can use SharedButtonComponent selector without error.Final Answer:
The button renders correctly with text 'Click' -> Option AQuick Check:
Imported shared module exports = component usable [OK]
- Not importing SharedModule in FeatureModule
- Forgetting to export component in SharedModule
- Using component selector incorrectly
@NgModule({
declarations: [SharedCardComponent],
imports: [CommonModule]
})
export class SharedModule {}
@NgModule({
imports: [SharedModule],
declarations: [DashboardComponent]
})
export class DashboardModule {}
DashboardComponent template uses <app-shared-card> but Angular throws an error.Solution
Step 1: Check SharedModule exports
SharedCardComponent is declared but not exported, so other modules can't use it.Step 2: Understand Angular module sharing rules
To use a component from another module, it must be exported by that module.Final Answer:
SharedCardComponent is not exported from SharedModule -> Option DQuick Check:
Declare + export needed to share component [OK]
- Forgetting to export declared components
- Trying to redeclare components in feature modules
- Misunderstanding imports vs exports
// SharedModule
@NgModule({
declarations: [SharedButtonComponent, CustomPipe],
exports: [SharedButtonComponent, CustomPipe],
imports: [CommonModule]
})
export class SharedModule {}
// FeatureModule
@NgModule({
imports: [SharedModule],
declarations: [FeatureComponent]
})
export class FeatureModule {}
What must you do in FeatureComponent template to use both?Solution
Step 1: Confirm shared module exports
SharedButtonComponent and CustomPipe are declared and exported in SharedModule, which FeatureModule imports.Step 2: Use correct selectors and pipe names
FeatureComponent can use selector and apply| customPipe(pipe names are case-sensitive and lowercase by convention).Final Answer:
Use <app-shared-button> and apply | customPipe in template expressions -> Option BQuick Check:
Import shared module + use selectors and pipe names correctly [OK]
- Redeclaring shared components or pipes in feature modules
- Using wrong selector or pipe name casing
- Trying to import components or pipes directly
Ready to prove it?
Pass the test and this pattern moves unknown
