What is the Angular CLI create component?

Angular components are the building blocks of the Angular framework.

Generating (or creating) a component in Angular using the CLI is pretty straight-forward.

Usage

To create a component named example, run the following command in the Angular CLI:

ng generate component example

The command performs the following tasks:

  • Creates a directory src/app/example.

  • Inside that directory four files are generated:

    • A CSS file for the component styles
    • An HTML file for the component template
    • A TypeScript file with a component class named ExampleComponent
    • A test file for the ExampleComponent class

The command also adds the ExampleComponent as a declaration in the @NgModule decorator of the src/app/app.module.ts file.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved