angular
/

Angular Shared Modules – Reusable UI & Utilities

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

angular

Angular Shared Modules – Reusable UI & Utilities

The Shared Module Pattern

In a complex application like Revochamp, many feature modules need the same set of tools: standard buttons, loading spinners, custom date pipes, or third-party UI libraries like Angular Material. Instead of importing these into every single feature module, we create a Shared Module. As a Technical Lead, you use this pattern to maintain a 'Single Source of Truth' for your UI components and ensure consistency across the entire platform.

  1. Imports vs. Exports

The unique characteristic of a Shared Module is that it exports what it imports. When a feature module imports the SharedModule, it automatically gains access to everything in the exports array, even if those items weren't declared locally in the Shared Module.

TypeScriptRead-only
1
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatButtonModule } from '@angular/material/button';
import { CustomSpinnerComponent } from './spinner.component';

@NgModule({
  declarations: [CustomSpinnerComponent],
  imports: [
    CommonModule, 
    MatButtonModule
  ],
  exports: [
    CommonModule,         // Re-exporting for convenience
    MatButtonModule,      // Re-exporting UI library
    CustomSpinnerComponent // Re-exporting local UI component
  ]
})
export class SharedModule { }

  1. What Belongs in SharedModule?

A well-architected Shared Module should only contain 'dumb' or 'presentational' items that don't rely on application state. If a component is highly specific to a business workflow, it belongs in a Feature Module, not Shared.

  • Reusable Components: Custom buttons, input wrappers, modal headers, and cards.
  • Directives: Tooltips, hover effects, or input masks.
  • Pipes: Currency formatters, date transformers, or string truncators.
  • Third-party Modules: Angular Material, FontAwesome, or NgCharts.

  1. The 'No Providers' Rule

As an Architect, it is critical to avoid putting services in the providers array of a Shared Module. If a Shared Module with providers is imported by multiple lazy-loaded modules, each lazy module will get its own separate instance of the service, breaking the singleton pattern. Services should live in a CoreModule or use providedIn: 'root'.

Module Roles Comparison

FeatureShared ModuleCore ModuleFeature Module
ComponentsReusable / UI onlyNone (usually)Domain specific
ServicesAvoid (No providers)Global SingletonsFeature specific
Imported byMultiple Feature ModulesRoot Module onlyRoot (Eager) or Router (Lazy)
GoalDRY UI & UtilitiesApp-wide InfrastructureBusiness Logic / Workflow

Test Your Knowledge

Q1
of 3

What is the primary purpose of the 'exports' array in a Shared Module?

A
To hide components from other modules
B
To make imported modules and local components available to other modules
C
To define backend API endpoints
D
To register global services
Q2
of 3

Why should you avoid putting 'providers' in a Shared Module that is lazy-loaded?

A
It causes a compilation error
B
It prevents the app from starting
C
It creates multiple instances of a service instead of a singleton
D
It makes the CSS stop working
Q3
of 3

Which of these is the most appropriate item to put in a Shared Module?

A
A login service with user credentials
B
The main admin dashboard layout
C
A reusable 'PrimaryButton' component
D
App routing configuration

Frequently Asked Questions

Does SharedModule make the app bundle larger?

No. Modern build tools use tree-shaking to ensure only the code you actually use is included. However, keep the SharedModule focused; if it becomes too large, consider splitting it into 'SharedUiModule', 'SharedPipesModule', etc.

Can I import SharedModule into the Root Module?

Yes, but you usually don't need to. The Root Module usually only needs the shell components. Feature modules are the primary consumers of SharedModule.

What is the difference between CommonModule and SharedModule?

CommonModule is an Angular-provided module with core directives (*ngIf, *ngFor). SharedModule is a custom module you create to group CommonModule and your own reusable assets together for easier importing.

Previous

angular feature modules

Next

angular rxjs

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.