Sub topic 4
What belongs in core and shared
This chapter assumes you know standalone components, dependency injection, and how to set up providers in app.config.ts. Feature folders hold code that belongs to one business area. Some code serves the whole app, and it goes in two folders: core and shared. They have different jobs, and mixing them up is a common way for a feature-based structure to decay.
Two folders, two questions
Ask one question per piece of code.
- Core: "Does the app need exactly one of these, running for its whole lifetime?"
- Shared: "Do several features need to reuse this, with no app-wide state?"
| Folder | Holds | Typical contents |
|---|---|---|
| `core` | App-wide singletons and setup | Auth service, HTTP interceptors, error handler, app config, layout shell |
| `shared` | Stateless, reusable building blocks | Dumb UI components, pipes, directives, small utilities |
Dependency direction
Features may import from core and shared. Core may import from shared. Shared imports from neither. Neither core nor shared may import from a feature.
If shared imports a feature, you get a cycle and the feature can no longer be loaded on its own.
What goes in core
Core code is registered once, at the root. An auth interceptor is a typical example.
// src/app/core/auth/auth.service.ts
import { Injectable, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class AuthService {
readonly token = signal<string | null>(null);
}// src/app/core/auth/auth.interceptor.ts
import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { AuthService } from './auth.service';
export const authInterceptor: HttpInterceptorFn = (req, next) => {
const token = inject(AuthService).token();
return token
? next(req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }))
: next(req);
};// src/app/app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { authInterceptor } from './core/auth/auth.interceptor';
export const appConfig: ApplicationConfig = {
providers: [provideHttpClient(withInterceptors([authInterceptor]))],
};Features never register these. They only benefit from them.
What goes in shared
Shared code knows nothing about your business domain. A pipe that formats a file size qualifies. A component that shows an order status does not, because it belongs to the orders feature.
// src/app/shared/pipes/file-size.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'fileSize' })
export class FileSizePipe implements PipeTransform {
transform(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
}Because the pipe is standalone, a component imports it directly in its imports array. No shared module is needed.
Common errors
- Shared becomes a dumping ground. Code lands there "just in case". Keep it in its feature until a second feature needs it.
- Stateful services in shared. A
providedIn: 'root'service insharedis a hidden singleton. If it holds state, it belongs in core or inside a feature. - Domain types in shared. An
Orderinterface insharedties every consumer to one feature's model. - Core that grows into a second app. Core should stay small. Feature-specific logic does not become core logic because many pages use it.
Exercise
A CurrencyBadgeComponent is used by the orders and invoices features. It takes an amount and a currency code, and has no injected services. Another class, CartService, holds the user's current cart and is read by the header, the checkout page and the orders feature. Where does each one go, and why?
Show answer
CurrencyBadgeComponent goes in shared. Several features reuse it, it is stateless, and it has no domain dependencies. CartService holds app-wide state that must exist once, so it belongs in core. A stronger option is to place it in a cart feature and expose it through that feature's public API, which later chapters cover. It does not belong in shared, because shared code must carry no state.