Sub topic 6
The root component and standalone components
This chapter assumes you have read the bootstrapping chapter and know basic TypeScript decorators. It explains what the root component is and how standalone components declare their own dependencies.
The root component
The root component is the first component Angular creates. bootstrapApplication(AppComponent, appConfig) in main.ts receives the class, and Angular renders it where index.html contains the matching selector.
<body>
<app-root></app-root>
</body>The root component sits at the top of the component tree. Every other component is created inside its template, directly or through the router.
What a generated root component looks like
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
export class AppComponent {
title = 'my-app';
}The selector must match the tag in index.html. The imports array lists what the template uses. Here that is RouterOutlet, the directive that displays the component for the current route.
Standalone components
A standalone component declares its own template dependencies in its imports array. Older Angular code used NgModules for this. A module's declarations array registered components, and the module's imports made other directives available to them.
Standalone components need no module. Anything the template uses must be imported on that component: other components, directives, pipes, and some utilities.
Angular 19 and later treat every component as standalone by default. In Angular 17 and 18 the generated code includes standalone: true in the decorator, and you must keep it.
Using another component
Suppose you generated products/product-list with ng generate component. To show it in the root template, import the class and add it to imports.
import { Component } from '@angular/core';
import { ProductListComponent } from './products/product-list/product-list.component';
@Component({
selector: 'app-root',
imports: [ProductListComponent],
template: '<h1>Shop</h1><app-product-list />',
})
export class AppComponent {}Importing the class in the file is not enough. The class must also appear in the imports array, or the compiler rejects the template.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `'app-product-list' is not a known element` | The component is not in `imports` | Add the class to the `imports` array |
| Blank page, no error in the template | `selector` differs from the tag in `index.html` | Make them match |
| Error about `declarations` or "is standalone" | A standalone component was put in a module's `declarations` | Put it in `imports` instead |
Exercise
You add <app-cart /> to the root template and the build fails with "'app-cart' is not a known element". The CartComponent file exists and is imported at the top of app.component.ts. What is missing?
Show answer
CartComponent must be added to the imports array of the @Component decorator on AppComponent. A TypeScript import alone does not make the component available to the template.