AI Hints
Home›Angular Tutorial›The Root Component, ng serve and Your First Change
Angular · Lesson 7 of 120

The Root Component

Where it all starts.

⏱ 2 min read▶ Video at 7:21🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Every Angular app starts with one component, the root component. All the other components will live inside it.

  • One root component
  • Everything lives inside
  • app.ts

src/app/app.ts

This is the root component, in app dot ts. The at Component decorator turns a normal class into a component. Selector app root matches the tag in index dot html. Template url points to the HTML file, and style url points to the CSS file. The class holds the component's data. Here, title is a signal, holding the text shop.

app.tstypescript
import { Component, signal } from '@angular/core';
import { RouterOutlet } from '@angular/router';

@Component({
  imports: [RouterOutlet],
  selector: 'app-root',
  styleUrl: './app.css',
  templateUrl: './app.html',
})
export class App {
  protected readonly title = signal('shop');
}

The parts of a component

A component is a TypeScript class, with a decorator. The selector is the tag name, used to show it. The template is the HTML it draws. And styles make it look good, only inside this component.

keyvalue
Classdata and logic
selectorits tag: <app-root>
templatethe HTML it draws
stylesCSS for this component only

Three files, one component

The CLI puts a component in three files. A ts file for the class, an html file for the template, and a css file for styles. They always sit side by side. For small components, the template can live inside the ts file instead, as we'll often do.

keyvalue
app.tsthe class
app.htmlthe template
app.cssthe styles
Small componentstemplate: `...` inline

Summary

The root component, App, has a selector, a template, and styles. Angular puts it into the app root tag. Next, running the app, with ng serve.

Key takeaways
  • @Component
  • selector: 'app-root'
  • templateUrl, styleUrl
Next up: ng serve — run the app