Skip to main content

Why Components

One of the main concepts in Angular is that your application is split into a tree of components. Each component represents a reusable piece of the UI that:

  • can contain other components โ€” its children,
  • communicates with its children through a defined contract,
  • exposes a contract to its parent,
  • keeps its state isolated from other components.

The main advantages of components are:

  • Reusability โ€” components are designed to be reusable.
  • Isolation โ€” components keep their implementation isolated from other components.
  • Separation of Concerns โ€” each component has a specific role and delegates work to other components.
  • Testability โ€” components help narrowing down tests.

All these elements are meant to make apps easier to maintain.

A Component controls a patch of screen real estate that we could call a view, and declares reusable UI building blocks for an application.

โ€” Brad Green (but I'm not sure it was him ๐Ÿ˜…)

Kitchen Discipline Pays Off

Note that well crafted components are like a well organized fridge and kitchen, they will help you ship faster.

Exampleโ€‹

Let's take our cooking app Whiskmate.

Whiskmate

We can imagine the following component hierarchy:

Pragmatic Angular Testing
๐Ÿ‘จ๐Ÿปโ€๐Ÿณ Let's cook some tests โ†’

๐Ÿ’ฐ 80โ‚ฌ ยท 170โ‚ฌ ยท Lifetime access

Learn how to write reliable tests that survive upgrades and refactorings.