Sub topic 2
Standalone components as the unit of composition
This chapter assumes you know TypeScript, basic Angular components and templates, dependency injection, and the Angular CLI. It covers how standalone components make a single component the building block of a feature.
Why standalone matters for structure
Before standalone components, every component belonged to an NgModule. The module listed what the component could use in its template. To find out what a component depended on, you had to open a second file.
A standalone component declares its own dependencies. It lists the components, directives and pipes it uses in its imports array. The component file shows its dependencies directly, so you can move or delete a component without editing a module.
A standalone component
import { Component, input } from '@angular/core';
import { CurrencyPipe, DatePipe } from '@angular/common';
import { RouterLink } from '@angular/router';
export interface OrderSummary {
id: string;
total: number;
placedAt: Date;
}
@Component({
selector: 'app-order-card',
imports: [CurrencyPipe, DatePipe, RouterLink],
template: `
<a [routerLink]="['/orders', order().id]">Order {{ order().id }}</a>
<p>{{ order().total | currency }} on {{ order().placedAt | date }}</p>
`,
})
export class OrderCardComponent {
readonly order = input.required<OrderSummary>();
}Recent Angular versions treat components as standalone by default, so you do not need standalone: true. In older versions (before v19), add standalone: true to the decorator.
The template uses routerLink, currency and date. Each one is named in imports. If you leave one out, the compiler reports an error for the unknown pipe or the unrecognized binding.
Composing components
A parent component composes children by adding them to its own imports array.
import { Component } from '@angular/core';
import { OrderCardComponent, OrderSummary } from './order-card.component';
@Component({
selector: 'app-order-list',
imports: [OrderCardComponent],
template: `
@for (order of orders; track order.id) {
<app-order-card [order]="order" />
}
`,
})
export class OrderListComponent {
readonly orders: OrderSummary[] = [
{ id: 'A-100', total: 42.5, placedAt: new Date('2026-01-15') },
{ id: 'A-101', total: 19.99, placedAt: new Date('2026-02-03') },
];
}Imports follow the template. If a component stops using a child, remove the import. The compiler warns about unused standalone imports in current versions.
How this supports feature structure
Three properties make standalone components a good unit for features:
- Local dependencies. A component names everything it needs, so a feature folder can be understood without a module file.
- Direct lazy loading. The router can load one component on demand with
loadComponent. The next chapters use this to split features into separate bundles. - Explicit boundaries. To use a component from another feature, you must import it by name. That import is visible, and later chapters show how to restrict it.
Application-wide providers move to bootstrapApplication:
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter } from '@angular/router';
import { provideHttpClient } from '@angular/common/http';
import { AppComponent } from './app/app.component';
import { routes } from './app/app.routes';
bootstrapApplication(AppComponent, {
providers: [provideRouter(routes), provideHttpClient()],
});Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Error about an unknown element such as `app-order-card` | The child component is missing from `imports` | Add the child class to the parent's `imports` |
| Error about a missing pipe such as `currency` | The pipe is not imported | Import `CurrencyPipe` in the component |
| Error that a component is not standalone | An older component is still declared in an `NgModule` | Import its module, or convert it to standalone |
| Large `imports` arrays in every component | Shared pieces are imported one by one | Group related shared pieces in a constant array, covered in the core and shared chapter |
Exercise
A component's template uses <app-order-card> and the date pipe, but the component's imports array is empty. What happens, and how do you fix it?
Show answer
The compiler reports an unknown element and an unknown pipe. Add OrderCardComponent and DatePipe to the component's imports array.
Exercise
Why does a standalone component work better than a module-declared component for loadComponent lazy loading?
Show answer
A standalone component carries its own template dependencies, so the router can load it alone without a module that declares it.