Sub topic 2
Standalone components as the unit of composition
This chapter assumes you know TypeScript, can write a basic Angular component with a template and inject a service, and have used the Angular Router. It uses Angular 19 or later, where components are standalone by default. On Angular 17 or 18, add standalone: true to each @Component decorator in the examples.
The working model
Before standalone components, every component belonged to an NgModule. The module's declarations array decided which template elements, pipes, and directives the component could use. A component could not say what it needed. The module said it for the whole group.
A standalone component carries that information itself. Its imports array lists everything its template uses: other components, directives, pipes, and, if needed, whole NgModules. Nothing else is in scope.
This matters for feature structure because the component becomes the smallest unit you can move, lazy-load, test, and review. You can read one file and see what the component depends on.
What a standalone component declares
The examples use a small model for an order. It is deliberately minimal.
// orders/data-access/order.model.ts
export interface Order {
id: string;
total: number;
createdAt: string;
}The first component is a presentational one. It receives an order through a signal input and renders it. It imports exactly the pipes and the directive its template uses.
// orders/ui/order-summary.component.ts
import { ChangeDetectionStrategy, Component, input } from '@angular/core';
import { CurrencyPipe, DatePipe } from '@angular/common';
import { RouterLink } from '@angular/router';
import { Order } from '../data-access/order.model';
@Component({
selector: 'app-order-summary',
imports: [CurrencyPipe, DatePipe, RouterLink],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<a [routerLink]="['/orders', order().id]">Order {{ order().id }}</a>
<span>{{ order().total | currency }}</span>
<span>{{ order().createdAt | date: 'mediumDate' }}</span>
`,
})
export class OrderSummaryComponent {
readonly order = input.required<Order>();
}If you remove DatePipe from imports, the template no longer compiles. The compiler reports that the pipe date could not be found. The same applies to elements: a missing component import produces an "is not a known element" error.
Template scope and dependency injection are separate
Intermediate developers often mix up two systems that standalone components keep apart.
| Question | Mechanism | Where you configure it |
|---|---|---|
| Which tags, pipes, and directives can this template use? | Template scope | The component's `imports` array |
| Which services can this class `inject()`? | Dependency injection | `providers` on a route, a component, or the application |
Importing OrderSummaryComponent into another component gives that component a new tag to use. It does not provide any service the child needs. If a child injects OrdersStore, some injector above it must provide that store. The next chapters cover lazy routes and route-scoped providers, which are the right place for that.
Composing a feature page
A feature page composes the ui components and connects them to the feature's store. The store here is the one from the plan: an @Injectable() with no providedIn, provided by the route.
// orders/feature/order-list.component.ts
import { Component, inject } from '@angular/core';
import { OrdersStore } from '../data-access/orders.store';
import { OrderSummaryComponent } from '../ui/order-summary.component';
@Component({
selector: 'app-order-list',
imports: [OrderSummaryComponent],
template: `
<h1>Orders ({{ store.count() }})</h1>
@for (order of store.orders(); track order.id) {
<app-order-summary [order]="order" />
} @empty {
<p>No orders yet.</p>
}
`,
})
export class OrderListComponent {
protected readonly store = inject(OrdersStore);
constructor() {
this.store.load();
}
}The imports list is short and local. OrderListComponent names one child and one service. The built-in @for and @empty blocks need no import, because control flow is part of the template syntax. Only the older structural directives *ngFor and *ngIf require NgFor and NgIf in imports.
The page works only if OrdersStore is provided above it. The route file from the plan does that with providers: [OrdersStore] and then lazy-loads this component with loadComponent. The component never needs to know which route or injector supplies the store.
Bootstrapping without a root module
An application without AppModule starts with bootstrapApplication. Application-wide providers go in its second argument, in place of imports: [HttpClientModule] and similar.
// main.ts
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()],
}).catch((err) => console.error(err));Anything listed here lands in the root injector. Keep it small and limited to true singletons such as the router, the HTTP client, and an auth session. Feature services belong in route providers, so they are created and discarded with the feature. Chapter 4 covers what counts as core.
Rules of thumb for `imports`
These habits keep component dependencies honest as the app grows.
- Import only what the template uses. An unused import is dead weight that tools can flag.
- Import the specific pipe or directive, such as
CurrencyPipe, not a large shared array. A bundle of "everything" re-creates the old shared module and hides real dependencies. - If several components always need the same group of items, export a constant array from
sharedand spread it intoimports. Do this only for items that genuinely belong together, like a small set of form directives. - Import from the feature's own folders by relative path. Reach into another feature only through its public API, which a later chapter defines.
- Prefer
input(),output(), and signals for the component's contract. They make the data flow in and out visible in the class body.
Common errors
"'app-order-summary' is not a known element." The parent's imports array lacks the child. Add the class, not the selector, to imports. In a template that uses a selector from a standalone component, the compiler checks this at build time, so you see the error before running the app.
The component is imported but nothing renders, and no error appears. Check for a custom element schema or a wrapper component. If the child's selector is correct and imported, also check that the parent actually renders the tag in the template and not in a conditional block that never runs.
"No provider for OrdersStore." The component injects a service that no injector above it provides. Importing a component does not import its providers. Provide the service in the route, in the component's own providers, or in the application.
A circular import between two components. A parent that imports a child, where the child imports the parent, fails at compile time or at module loading. Recursive UI such as a tree needs one component that refers to itself in its own template, which is allowed, or a split into a container and a node component. If two feature components import each other, that is a boundary problem, not a syntax one.
Components in the wrong layer. A ui component that injects a feature store or calls HttpClient is no longer reusable. Keep ui components dependent only on their inputs, outputs, and presentational imports. This is the rule the layering chapters later enforce with lint.
What to carry forward
A standalone component is a self-describing unit. Its imports array lists its template dependencies, and its injector context decides which services it reaches. The next chapter uses this unit to lay out feature folders. Later chapters add lazy loading and route-scoped providers on top of it.
Exercise
A teammate moves OrderSummaryComponent into a new folder and imports it in OrderListComponent. The build passes, but at runtime the page throws "No provider for OrdersStore". The route for the page has no providers array. Explain why importing the component did not help, and give two places where you could fix it.
Show answer
The imports array only controls which template elements, pipes, and directives are in scope. It does not carry services. OrdersStore has no providedIn, so some injector above the page must provide it. Fix it by adding providers: [OrdersStore] to the feature's route (the preferred option, since the store then lives and dies with the feature), or by adding it to the providers of the page component itself, which creates one instance per page instance. Providing it in bootstrapApplication would also work, but it makes a feature service app-wide, which defeats the structure.
Exercise
A template uses {{ price | currency }} and <a routerLink="/cart">, but the component's imports array is empty. Name the two symbols you must add, and the packages they come from.
Show answer
Add CurrencyPipe from @angular/common and RouterLink from @angular/router. The component then compiles, because both are now in the template's scope.