AI Hints
Home›Angular Tutorial›Using Components Together, Component Styles and Practice: A Book Card
Angular · Lesson 25 of 120

Using Components Together

Components inside components.

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

A component on its own isn't very useful. The power comes from putting components inside other components, like building blocks.

  • One component alone
  • Components inside components
  • Building blocks

book-card.ts

Here's a small book card. Its selector is app book card. The template shows a title, and an author, inside a card.

book-card.tstypescript
import { Component } from '@angular/core';

@Component({
  selector: 'app-book-card',
  template: `
    <div class="card">
      <h3>Dune</h3>
      <p>Frank Herbert · £9.99</p>
    </div>
  `,
})
export class BookCard {}

Using it in App

First, App imports the Book Card class, from its file. Then the imports array in the decorator, tells Angular this template may use it. Now the template can use the app book card tag, as many times as we like. Run it. Two book cards appear, under the heading.

app.tstypescript
import { Component } from '@angular/core';
import { BookCard } from './book-card';

@Component({
  selector: 'app-root',
  imports: [BookCard],
  template: `
    <h1>New this week</h1>
    <app-book-card />
    <app-book-card />
  `,
})
export class App {}
Also uses book-card.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-book-card',
  template: `
    <div class="card">
      <h3>Dune</h3>
      <p>Frank Herbert · £9.99</p>
    </div>
  `,
})
export class BookCard {}

Forgetting the imports array

This time, we forget the imports array. Run it. The build fails. Angular says, app book card is not a known element. So when you see that error, check the imports array first.

app.tstypescript
import { Component } from '@angular/core';
import { BookCard } from './book-card';

@Component({
  selector: 'app-root',
  template: `
    <h1>New this week</h1>
    <app-book-card />
    <app-book-card />
  `,
})
export class App {}
Output
✘ [ERROR] NG8001: 'app-book-card' is not a known element:
1. If 'app-book-card' is an Angular component, then verify that it is included in the
'@Component.imports' of this component.
2. If 'app-book-card' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the
'@Component.schemas' of this component to suppress this message. Find more at
https://next.angular.dev/errors/NG8001

    src/app/app.ts:8:4:
      8 │     <app-book-card />
        ╵     ~~~~~~~~~~~~~~~~~
Also uses book-card.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-book-card',
  template: `
    <div class="card">
      <h3>Dune</h3>
      <p>Frank Herbert · £9.99</p>
    </div>
  `,
})
export class BookCard {}

Summary

To use a component, import its class, add it to the imports array, and use its tag in the template. Next, component styles.

Key takeaways
  • import { BookCard }
  • imports: [BookCard]
  • <app-book-card />
Next up: Component Styles — CSS that stays inside