Sub topic 3
Laying out feature folders
This chapter assumes you can write standalone Angular components, use dependency injection, and read a routes array. It also assumes you can run the Angular CLI. Here you will decide what a feature folder contains and how to name and shape it. Lazy loading, route-scoped providers, and barrels each get their own chapter later, so they appear below only as far as the layout needs them.
What a feature folder is
A feature folder holds everything needed for one business capability, such as orders, customers, or invoicing. It contains the screens, the services that feed them, the models they share, and their tests. A person working on "orders" should rarely need to leave src/app/orders/.
The opposite layout groups by file type: one components/ folder, one services/ folder, one models/ folder. That layout works for a dozen files. At a few hundred files, a single change touches three distant folders. Deleting a feature means hunting for its pieces across the tree.
The key idea is colocation: files that change together live together.
A starting layout
This is a layout for an app with two features and the shared code around them.
src/app/
├── app.component.ts
├── app.config.ts
├── app.routes.ts
├── core/
│ ├── auth/
│ └── layout/
├── shared/
│ ├── ui/
│ └── util/
├── orders/
│ ├── index.ts
│ ├── orders.routes.ts
│ ├── data-access/
│ │ ├── order.model.ts
│ │ ├── orders.store.ts
│ │ └── orders.store.spec.ts
│ ├── feature/
│ │ ├── order-list.component.ts
│ │ ├── order-list.component.html
│ │ ├── order-list.component.spec.ts
│ │ └── order-detail.component.ts
│ ├── ui/
│ │ └── order-row.component.ts
│ └── util/
│ └── order-totals.ts
└── customers/
└── ...Each part has a job:
app.routes.tsmaps URL prefixes to features and nothing else.orders.routes.tsis the feature's entry point. It declares the feature's routes and the providers that belong to it.index.tsis the barrel, the feature's public API. A later chapter covers it in detail.- The four sub-folders (
data-access,feature,ui,util) sort files by role inside the feature. core/andshared/hold code that does not belong to a single feature. The next chapter covers what goes there.
Start smaller if you need to. A feature with three files does not need four sub-folders. A flat orders/ folder is fine until it passes roughly ten files. Splitting early adds navigation cost without adding clarity.
The role sub-folders
The sub-folder names describe what a file does, not what Angular calls it.
| Folder | Contents | May depend on |
|---|---|---|
| `feature/` | Routed components that wire the screen together. They inject stores and render `ui` components. | `ui`, `data-access`, `util` |
| `ui/` | Presentational components. They take inputs, emit outputs, and inject no feature services. | `ui`, `util` |
| `data-access/` | Stores, API calls, and models. | `util` |
| `util/` | Pure functions and constants with no Angular dependency where possible. | nothing |
These dependency directions are the same ones a lint rule can enforce later in the tutorial. Following them by convention now makes that step easy.
A feature, file by file
The feature's route file is its front door. It loads the screens lazily and provides the store for the feature's routes only.
// src/app/orders/orders.routes.ts
import { Routes } from '@angular/router';
import { OrdersStore } from './data-access/orders.store';
export const ORDERS_ROUTES: Routes = [
{
path: '',
providers: [OrdersStore],
children: [
{
path: '',
loadComponent: () =>
import('./feature/order-list.component').then((m) => m.OrderListComponent),
},
{
path: ':id',
loadComponent: () =>
import('./feature/order-detail.component').then((m) => m.OrderDetailComponent),
},
],
},
];The model lives in data-access/ because the store and the API shape define it:
// src/app/orders/data-access/order.model.ts
export interface Order {
id: string;
customerName: string;
totalCents: number;
}A routed component in feature/ injects the store and calls it:
// src/app/orders/feature/order-list.component.ts
import { Component, inject, OnInit } from '@angular/core';
import { OrdersStore } from '../data-access/orders.store';
@Component({
selector: 'app-order-list',
standalone: true,
template: `
<h1>Orders ({{ store.count() }})</h1>
<ul>
@for (order of store.orders(); track order.id) {
<li>{{ order.customerName }}</li>
}
</ul>
`,
})
export class OrderListComponent implements OnInit {
protected readonly store = inject(OrdersStore);
ngOnInit(): void {
this.store.load();
}
}Notice the relative imports. Inside a feature, files import their neighbors with relative paths such as ../data-access/orders.store. Aliases are for crossing feature boundaries, not for moving around inside one.
How the pieces connect
The arrows show the allowed direction of imports. The two features sit side by side and have no arrow between them. That absence is deliberate: features that import each other's internals become one big feature again.
Deciding what counts as a feature
A good feature boundary usually matches one of these:
- A top-level navigation item, such as "Orders" in the sidebar.
- A noun the business uses, such as customer, invoice, or shipment.
- A set of screens that one team or one person typically changes together.
A poor boundary matches a technical concern instead, such as forms/, dialogs/, or http/. Those are layers, and they belong in shared/ or core/.
Test a boundary with one question: if the product team dropped this capability tomorrow, could you delete one folder and one route entry? If you would also edit six other folders, the boundary is wrong.
Edge cases
Nested features. A large feature can contain sub-features, for example orders/returns/. Give each sub-feature the same shape as a top-level one, with its own routes file. Keep nesting to two levels. Deeper trees make imports and routes hard to follow.
Code two features need. Do not let customers/ import from orders/data-access/. First ask whether the code is truly shared. If it is generic, such as a currency pipe, move it to shared/util. If it is domain logic that both features rely on, you may have found a third feature, or you may need the communication patterns from a later chapter.
Shared UI that grows. A button used by every feature belongs in shared/ui. A component that renders an order summary and is used in two features does not. Keep it in orders/ui and expose it through the feature's public API, so ownership stays clear.
Spec files. Place *.spec.ts files next to the file they test. Colocated tests move and get deleted with the code they cover.
One component per file. The CLI default of one class per file still applies. Splitting by feature does not change that rule.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| A `shared/` folder that holds everything used twice | `shared/` grows faster than any feature and imports feature types | Move domain code back to its owning feature. Keep `shared/` generic. |
| `app.routes.ts` that lists every screen | Large file, and every feature is bundled at startup | Map one prefix per feature and load each lazily. |
| Reaching into `../../orders/data-access/...` from another feature | Renaming a file in orders breaks customers | Import only from the feature's barrel. |
| Folders named after Angular artifact types inside features, such as `components/`, `services/`, `pipes/` | The by-type layout returns inside each feature | Use role folders such as `feature/` and `data-access/`, or stay flat while small. |
| Splitting into role folders for a three-file feature | Extra navigation, no benefit | Keep it flat until it grows. |
Putting it together
- Pick the feature by a business noun or a navigation item.
- Create
src/app/<feature>/with a<feature>.routes.tsfile. - Add role sub-folders only when the file count justifies them.
- Keep imports inside the feature relative.
- Register the feature once in
app.routes.tsby prefix.
Exercise
You are adding an "invoices" feature. It has a list screen, a detail screen, an Invoice model, a store that calls the API, and a helper that formats invoice numbers as INV-0042. Another feature, "customers", also needs to show invoice numbers. List the folder structure for invoices and say where the formatting helper should live. Explain your choice.
Show answer
Create src/app/invoices/ with invoices.routes.ts, index.ts, data-access/ (invoice.model.ts, invoices.store.ts), and feature/ (invoice-list.component.ts, invoice-detail.component.ts), plus spec files beside each file. A ui/ folder is only needed if you extract presentational components. The formatter is a pure function with no Angular dependency, so it fits in invoices/util/format-invoice-number.ts. Because customers also needs it, you have two options. If the formatting is invoice-specific domain logic, export it from the invoices barrel so customers imports it through the public API and not through a deep path. If it is a generic formatting rule that has nothing to do with invoices, move it to shared/util. Importing invoices/util/... directly from customers is the wrong answer, because it bypasses the feature's public API.
Exercise
A teammate puts a DialogService and a ConfirmDialogComponent in orders/feature/ because orders was the first feature to need them. Now customers needs them too. What should happen to them, and why?
Show answer
They are generic and not owned by the orders domain, so move them out of orders. The component, which is presentational, goes to shared/ui. If the service holds app-wide behavior and should exist once, core/ is the better place for it. Customers can then import them without depending on orders, which keeps the two features independent.