Sub topic 15
Organizing code as the application grows
This chapter assumes you can create a workspace with ng new, you know what main.ts and app.config.ts do, and you have used ng generate at least once. It covers how to arrange files in src/app so the project stays readable as features pile up.
Why the default layout stops working
ng new puts every file directly in src/app. That is fine for a root component and a routes file. After a dozen components and services, you can no longer tell which files belong together. Angular does not force a structure, so the arrangement is your decision.
Organize by feature, not by type
A type-based layout groups files by kind: one folder for all components, one for all services. A feature-based layout groups files by what they do for the user. Feature folders are easier to navigate, because a change to "products" touches one folder.
src/app/
app.ts
app.config.ts
app.routes.ts
core/
auth.service.ts
shared/
price.pipe.ts
products/
product-list/
product-list.ts
product-detail/
product-detail.ts
product.service.ts
products.routes.ts(Older CLI versions name the root file app.component.ts.)
The common folders
| Folder | Holds | Rule of thumb |
|---|---|---|
| Feature folders such as `products/` | Components, services and routes for one area | Most code lives here |
| `shared/` | Pipes, directives and small components reused by several features | Must not depend on any feature |
| `core/` | App-wide singleton services, such as authentication | Used by the whole app, imported by features |
Keep the dependency direction one-way. Features may import from shared/ and core/. shared/ and core/ should never import from a feature. Otherwise you create circular imports that are hard to untangle.
Let ng generate place the files
The CLI accepts a path as part of the name, and it creates missing folders for you.
ng generate component products/product-detail
ng generate service products/product
ng generate pipe shared/priceThe generated files land under src/app/ in the folder you name. Because components are standalone, there is no module file to update. You import what each component needs in its own imports array.
Give each feature its own routes file
Instead of listing every route in app.routes.ts, let each feature export its own array and load it lazily.
// src/app/products/products.routes.ts
import { Routes } from '@angular/router';
export const PRODUCT_ROUTES: Routes = [
{
path: '',
loadComponent: () =>
import('./product-list/product-list').then((m) => m.ProductList),
},
];// src/app/app.routes.ts
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'products',
loadChildren: () =>
import('./products/products.routes').then((m) => m.PRODUCT_ROUTES),
},
];loadChildren with a dynamic import() makes the build tool split that feature into a separate chunk, which the browser downloads only when someone visits /products. Adjust the file and class names to match your CLI version (product-list.component and ProductListComponent in older projects).
Common mistakes
- Putting every service in
shared/. Reserve it for reusable UI pieces and helpers. - Letting
shared/import a feature service, which creates a cycle. - Registering feature routes eagerly in
app.routes.ts, which loses lazy loading.
Exercise
You need a CartService used only by the checkout screens, and a TruncatePipe used by products, orders and checkout. Where should each go, and what command creates the pipe?
Show answer
Put CartService in the checkout/ feature folder, because only that feature uses it. Put TruncatePipe in shared/, because several features use it. Create the pipe with ng generate pipe shared/truncate.