Sub topic 3
Laying out feature folders
This chapter assumes you can write standalone Angular components, inject services, and define routes. It also assumes you know what a TypeScript import path looks like in a CLI project.
The working model
A feature folder holds everything needed for one business capability, such as orders or customers. It contains the components, routes, services, models and tests for that capability. A developer who works on orders should rarely need to leave the orders folder.
Compare this with grouping by file type, where components/, services/ and models/ each hold files from every capability. In that layout, one change touches several distant folders. In a feature layout, one change usually stays inside one folder.
A typical layout
src/app/
app.routes.ts
core/
shared/
orders/
index.ts
orders.routes.ts
feature/
order-list.component.ts
order-detail.component.ts
ui/
order-row.component.ts
data-access/
order.model.ts
orders.store.ts
util/
format-order-id.ts
customers/
index.ts
customers.routes.tsEach feature has an entry point and up to four subfolders:
| Path | Contents |
|---|---|
| `orders.routes.ts` | The feature's route table. The app-level routes file loads it lazily. |
| `feature/` | Routed components that coordinate a screen. They inject stores and compose `ui` components. |
| `ui/` | Presentational components that take inputs and emit outputs. They do not inject data services. |
| `data-access/` | Models, stores and HTTP calls. |
| `util/` | Pure functions with no Angular dependencies. |
| `index.ts` | The barrel file that lists what other code may import. |
Create only the subfolders you need. A small feature can start with orders.routes.ts and a couple of components. Add ui/ or util/ when the contents justify them.
Exposing the feature
The barrel file is the feature's public surface:
// src/app/orders/index.ts
export { ORDERS_ROUTES } from './orders.routes';
export type { Order } from './data-access/order.model';Other features and the app shell import from orders, never from orders/data-access/orders.store. Later chapters cover barrels and lint rules in detail. For now, treat everything not in index.ts as private.
Which direction imports may go
Imports point downward only. A ui component never imports from feature/. A util function never imports a component or a store.
Common errors
- Dumping everything into
shared/. If only orders code uses a component, keep it inorders/ui/. Move it toshared/only when a second feature needs it. - Importing across features by deep path.
import { OrdersStore } from '../orders/data-access/orders.store'couples the caller to a private file. Export a narrow API from the barrel, or let the feature own the interaction. - Mirroring the old type-based layout inside the feature. A feature with
components/,services/andmodels/folders works, but it hides the distinction between routed, presentational and data code. Thefeature/ui/data-access/utilsplit is easier to check with lint rules. - Forgetting the route file. Without
orders.routes.ts, the feature cannot be lazy-loaded as one unit.
Exercise
You have a StatusBadgeComponent currently in src/app/shared/. Only the orders feature uses it, and it takes a status string as an input. Where should it live, and which subfolder fits?
Show answer
Move it into src/app/orders/ui/. It is presentational, because it takes inputs and injects no data services, and only one feature uses it. Move it back to shared/ only when a second feature needs it.