Sub topic 2
Standalone Components and Composing Them with imports
This chapter assumes you know the component anatomy from the previous chapter (the @Component decorator, selector, template, and class). It also assumes you are comfortable with ES modules (import/export) and basic TypeScript.
What "standalone" means
A standalone component declares its own template dependencies directly, without an NgModule. From Angular 19, every component is standalone by default, so you don't write standalone: true any more. Older code that still uses NgModules marks its components with standalone: false.
A standalone component's template can only use these things:
- Plain HTML elements.
- Built-in template syntax, such as bindings and
@if/@for. - Components, directives, and pipes listed in its
importsarray.
Composing components
To use one component inside another, you need two imports. The first is a TypeScript import statement, which makes the class available to the file. The second is an entry in the imports array, which makes the selector available to the template.
// greeting.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-greeting',
template: `<p>Hello from a child component.</p>`
})
export class GreetingComponent {}// app.component.ts
import { Component } from '@angular/core';
import { UpperCasePipe } from '@angular/common';
import { GreetingComponent } from './greeting.component';
@Component({
selector: 'app-root',
imports: [GreetingComponent, UpperCasePipe],
template: `
<h1>{{ 'my app' | uppercase }}</h1>
<app-greeting />
<app-greeting />
`
})
export class AppComponent {}// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
bootstrapApplication(AppComponent).catch(err => console.error(err));bootstrapApplication renders the root component. Each child then brings in its own dependencies, which forms a tree.
What goes in imports
| Dependency | Example | Import from |
|---|---|---|
| Your components | `GreetingComponent` | its own file |
| Built-in pipes | `DatePipe`, `UpperCasePipe` | `@angular/common` |
| Built-in directives | `NgTemplateOutlet` | `@angular/common` |
| Form directives | `FormsModule` | `@angular/forms` |
| NgModules (legacy libraries) | `SomeLibModule` | the library |
You don't import anything for @if, @for, or @switch, because they are part of the template language itself.
Common errors
- "'app-greeting' is not a known element" means the component is missing from
imports. The TypeScriptimportstatement alone is not enough. - "No pipe found with name 'date'" means you forgot to add
DatePipetoimports. - The element renders as a blank tag with no error usually means a selector typo. Check the selector against the decorator. In strict template checking this shows up as the "not a known element" error.
- Importing
CommonModuleout of habit works, but importing only the specific pipes and directives you use keeps the dependencies explicit.
Exercise
A component template uses <app-avatar /> and {{ joined | date }}. The file has import { AvatarComponent } from './avatar.component'; at the top, but the browser reports that app-avatar is not a known element. What two changes fix the template?
Show answer
Add AvatarComponent to the decorator's imports array. Also add DatePipe, imported from @angular/common, so that the date pipe works too. The imports array would read imports: [AvatarComponent, DatePipe].