The Structure of a Component

A component is a TypeScript class marked with @Component, which gives its selector, template and styles. Generate one with ng generate component, and use it in another component by adding it to that component's imports and writing its selector as a tag.

Novice · Angular · Updated

After this lesson you can

  • Generate a component, and explain the parts of its @Component decorator
  • Use a component inside another by importing it and writing its selector

Key takeaways

  • ng generate component header, or ng g c header, creates a folder with the component's class, template, styles and tests.
  • The @Component decorator marks a class as a component: selector is its tag, templateUrl and styleUrl its files, and imports what its template uses.
  • To use a component in another, import its class at the top of the file, add it to the @Component imports list, and write its tag, such as <app-header />.
  • Error NG8001, app-header is not a known element, means the component is missing from the imports of the component whose template uses it.

Let's give Northwind's app a header.

Generating a component

In the project folder (stop ng serve with Ctrl+C, or open a second terminal):

ng generate component header
CREATE src/app/header/header.spec.ts
CREATE src/app/header/header.ts
CREATE src/app/header/header.css
CREATE src/app/header/header.html

(ng g c header is the short form.) The CLI makes a folder with the component's four files.

The class and its decorator

header.ts:

import { Component } from '@angular/core';

@Component({
  imports: [],
  selector: 'app-header',
  styleUrl: './header.css',
  templateUrl: './header.html',
})
export class Header {}
  • export class Header is an ordinary TypeScript class. export lets other files use it, like public on a C# class.
  • @Component({...}) is a decorator: it marks the class as a component and describes it, as [ApiController] described a controller. Its settings:
Setting Means
selector: 'app-header' The tag that shows this component: <app-header />. The app- prefix keeps your tags apart from real HTML ones.
templateUrl: './header.html' The file holding its HTML
styleUrl: './header.css' The file holding its styles
imports: [] The other components (and helpers) its template uses

Replace header.html's placeholder with the header itself:

<header class="bg-dark text-white p-3 mb-4">
  <h1 class="h3 m-0">Northwind Traders</h1>
</header>

Using it in App

To show the header, App's template uses its selector as a tag, and App imports the Header class: in the file, with import, and in its @Component imports list.

app.ts:

import { Component } from '@angular/core';
import { Header } from './header/header';

@Component({
  imports: [Header],
  selector: 'app-root',
  styleUrl: './app.css',
  templateUrl: './app.html',
})
export class App {}

Replace everything in app.html with:

<app-header />
<main class="container">
  <p>Welcome to Northwind's online store.</p>
</main>

Save, and the browser shows the dark header above the welcome line.

The two kinds of import do different jobs:

  • import { Header } from './header/header'; at the top is TypeScript's using: it makes the class name available in this file.
  • imports: [Header] in the decorator tells Angular that this component's template uses Header.

Forgetting the import

Leave Header out of imports, and Angular doesn't know the tag:

X [ERROR] NG8001: 'app-header' is not a known element:
1. If 'app-header' is an Angular component, then verify that it is included in the '@Component.imports' of this component.

The message says exactly what to do. Every component lists what its own template uses; nothing is shared by accident.

Check yourself

2 questions to try on the lesson page.

So what

A component is a class with a decorator: a selector to place it, a template and styles to draw it, and imports for what it uses. Generate one, import it where you need it, and write its tag.

Key terms

Cheat sheets