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.
- 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.
- 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.
- 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
| Feature | Shared Module | Core Module | Feature Module |
|---|---|---|---|
| Components | Reusable / UI only | None (usually) | Domain specific |
| Services | Avoid (No providers) | Global Singletons | Feature specific |
| Imported by | Multiple Feature Modules | Root Module only | Root (Eager) or Router (Lazy) |
| Goal | DRY UI & Utilities | App-wide Infrastructure | Business Logic / Workflow |