Video lecture
Watch on YouTube(opens in a new tab)Sub topic 2
Standalone Components and Composing Them with imports
This chapter assumes you already know the anatomy of a component: the @Component decorator, its selector, template, and the class behind it. You should also be comfortable with ES modules (import/export) and running an Angular CLI project with ng serve.
A single component is not an application. Real UIs are trees of components, and Angular has to know, for each template, which other components, directives, and pipes that template may use. Standalone components answer that question through the imports array.
What "standalone" means
A standalone component declares its own template dependencies directly in its decorator. It does not need to belong to an NgModule. Since Angular 19, every component, directive, and pipe is standalone by default. You no longer write standalone: true.
Before standalone components existed, you registered each component in an NgModule's declarations array. The module defined a shared "compilation scope": every component declared in a module could use everything that module declared or imported. With standalone components, each component has its own scope, and you can read that scope from its imports array.
| Aspect | Standalone component (default) | NgModule-declared component |
|---|---|---|
| Flag in decorator | None needed (Angular 19+) | `standalone: false` |
| Where dependencies are listed | The component's own `imports` | The module's `declarations` and `imports` |
| Scope of a template | Only what this component imports | Everything the module makes available |
| How the app starts | `bootstrapApplication(RootComponent)` | `platformBrowserDynamic().bootstrapModule(AppModule)` |
| Lazy loading a route | `loadComponent` | `loadChildren` pointing at a module |
Composing components with imports
To use one component inside another, you do two things:
- Import the class with a normal TypeScript
importstatement. - Add the class to the
importsarray of the component whose template uses it.
The TypeScript import makes the class available to the file. The imports array makes it available to the template compiler. You need both.
// greeting.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-greeting',
template: `<p>Hello from a child component.</p>`
})
export class GreetingComponent {}// profile-page.component.ts
import { Component } from '@angular/core';
import { UpperCasePipe } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { GreetingComponent } from './greeting.component';
@Component({
selector: 'app-profile-page',
imports: [GreetingComponent, UpperCasePipe, FormsModule],
template: `
<app-greeting />
<input [(ngModel)]="name" placeholder="Your name" />
<p>{{ name | uppercase }}</p>
`
})
export class ProfilePageComponent {
name = 'ada';
}The imports array accepts four kinds of things:
- Standalone components, such as
GreetingComponent. - Standalone directives, such as
NgClassor your own attribute directives. - Standalone pipes, such as
UpperCasePipeorDatePipe. NgModules, such asFormsModuleorReactiveFormsModule. Importing a module gives the template everything that module exports.
You will not find services in this list. Services are provided through dependency injection (providedIn: 'root', providers), not through imports. Adding a service class to imports causes a compile error.
The built-in control flow blocks (@if, @for, @switch) belong to the template syntax itself, so you never import them. You only need CommonModule or NgIf/NgFor if you still use the older structural directives.
Bootstrapping a standalone application
The root of the tree is also a standalone component. You start the application by passing it to bootstrapApplication. Application-wide providers go in an ApplicationConfig object.
// app.component.ts
import { Component } from '@angular/core';
import { ProfilePageComponent } from './profile-page.component';
@Component({
selector: 'app-root',
imports: [ProfilePageComponent],
template: `
<h1>My App</h1>
<app-profile-page />
`
})
export class AppComponent {}// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { ApplicationConfig } from '@angular/core';
import { AppComponent } from './app/app.component';
const appConfig: ApplicationConfig = {
providers: []
};
bootstrapApplication(AppComponent, appConfig).catch(err => console.error(err));The <app-root> element in index.html is where Angular renders AppComponent. Every other component is reached by following imports arrays down from there.
Scope is not inherited
AppComponent imports ProfilePageComponent, but that does not let AppComponent's template use GreetingComponent. In the other direction, GreetingComponent cannot use UpperCasePipe just because its parent imported it. Each template sees only its own component's imports. This design gives the compiler an exact dependency graph, and the build can then drop unused code.
Common errors and what they mean
Most mistakes in this area show up as compile-time errors with stable codes.
| Error | Typical message | Usual cause |
|---|---|---|
| NG8001 | `'app-greeting' is not a known element` | The component is missing from `imports`, or the selector is misspelled |
| NG8002 | `Can't bind to 'ngModel' since it isn't a known property of 'input'` | The directive that defines the input (here from `FormsModule`) is not imported |
| NG8004 | `No pipe found with name 'uppercase'` | The pipe class is missing from `imports` |
| NG8113 (warning) | `GreetingComponent is not used within the template` | An entry in `imports` that the template never uses |
Here are two less obvious problems:
- Circular imports. If component A imports B and B imports A, one class may still be
undefinedwhen the other's decorator runs. Wrap the reference:imports: [forwardRef(() => BComponent)]. A component that renders itself recursively, such as a tree node, can use its own selector without listing itself inimports. - Mixing worlds. A component with
standalone: falsecannot appear in a standalone component'simportsdirectly. Import theNgModulethat declares and exports it instead.
Standalone components and lazy loading
Every standalone component carries its own dependency list, so the router can load one on demand without a wrapper module:
// app.routes.ts
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'profile',
loadComponent: () =>
import('./profile-page.component').then(m => m.ProfilePageComponent)
}
];The build puts ProfilePageComponent and everything in its imports into a separate chunk, which loads only when the user navigates to /profile. To make this work, register the routes with provideRouter(routes) in appConfig.providers and place <router-outlet /> (from an imported RouterOutlet) in the root template.
Exercise
A template contains <app-greeting /> and {{ today | date }}. The file has import { GreetingComponent } from './greeting.component'; at the top, but the build fails with NG8001 and NG8004. What two changes fix it?
Show answer
Add GreetingComponent to the component's imports array, since the TypeScript import alone does not register it with the template compiler. Then import DatePipe from @angular/common and add it to imports too.
Exercise
AppComponent imports ProfilePageComponent, which imports UpperCasePipe. Can AppComponent's own template use {{ title | uppercase }} without further changes? Why?
Show answer
No. Template scope is not inherited. AppComponent's template sees only what AppComponent lists in its own imports, so it must import UpperCasePipe itself.
Exercise
You need a legacy component declared in LegacyWidgetsModule (with standalone: false) inside a new standalone component. What goes in the imports array?
Show answer
LegacyWidgetsModule, which must export the legacy component. You cannot list a non-standalone component in imports directly.