Sub topic 3
Importing Angular Features Through Stable Boundaries
Assume you know TypeScript, Angular dependency injection, standalone components, and the Angular Router. This chapter focuses on keeping feature dependencies explicit as an app grows.
Path aliases and public APIs
A feature should expose only the symbols that other parts of the app need. Put those exports in a barrel file:
// src/app/orders/index.ts
export { ORDERS_ROUTES } from './orders.routes';
export type { Order } from './data-access/order.model';Map the barrel to a stable alias in tsconfig.json:
{
"compilerOptions": {
"paths": {
"@app/orders": ["src/app/orders/index.ts"],
"@app/shared/*": ["src/app/shared/*"]
}
}
}Other code can now import Order from @app/orders instead of depending on the feature's internal folders. Keep the alias target at the feature boundary. Do not export every component or service by default.
Layering and dependency rules
Inside a feature, common layers have different jobs:
featurecontains screens and route-level composition.uicontains reusable presentation components.data-accesscontains API clients, models, and feature state.utilcontains small, domain-independent helpers.
A feature may depend on UI, data access, and utilities. A UI layer should not import a feature or data-access layer. This keeps presentation code reusable.
In an Nx workspace, lint rules can enforce this structure:
{
"@nx/enforce-module-boundaries": [
"error",
{
"depConstraints": [
{
"sourceTag": "type:ui",
"onlyDependOnLibsWithTags": ["type:ui", "type:util"]
}
]
}
]
}Communication, testing, and growth
Prefer shared contracts, URL navigation, or a small shared service when features must communicate. Avoid importing one feature's private store into another. A shared event or data-access library is often a better boundary.
Test a feature through its public routes and components. Mock HTTP and external providers, then verify behavior without loading unrelated features.
Large applications can move features into workspace libraries. Libraries add ownership and build boundaries, but they also add configuration. Start with folders, then extract a feature when its team, deployment, or dependency boundary justifies it.
For migration, choose one feature, create its folder and barrel, move its tests with it, and update imports through the alias. Add lint rules after the first boundaries are clear. Repeat feature by feature rather than rewriting the whole application.
Exercise
Where should a reusable button used by several features belong?
Show answer
Put it in a shared UI layer, then expose only its public component API.