How to Create Components in Angular 9

We can think of Angular Components like LEGO pieces. We create a component once but can use them multiple times in different
An Angular app is a tree structure consisting of all those components that we create, like how we make a Lego Structure from little Lego pieces.

Components are the most basic UI building block of an Angular app. An Angular app contains a tree of Angular components.— Angular official docs

In this article we will learn how to Create Components in Angular 9, use the generate component command of Angular CLI, or its shortcut shortcut command, for creating Angular components in your project.

How to Use ng generate component or ng g c

Let’s assume we want to create a Angular component named family
Open a new command-line interface, navigate into the root of your Angular project and run the following command:

$ ng generate component family

Or you can also run the following command:

$ ng g c family

Angular CLI will generate 4 files for the component in the src/app folder of your project:

/family/family.component.ts       # Component class
  /family/family.component.html     # Component template
  /family/family.component.css      # Component styles
  /family/family.component.spec.ts  # Component tests

We can also customize where the component’s files are placed.

Angular CLI adds the component to the declarations array of the module.

@NgModule({
  // [...]
  declarations: [ AppComponent, FamilyComponent ]
  // [...]
})
export class AppModule { }

This will allow FamilyComponent in any component in the AppModule.

How to Customize the Output of ng generate component

By default the ng generate component or ng g c commands, generate a folder and four files for the component. But you can change the default behavior if you want in two ways:

Using flags with the command,
Using the angular.json configuration file.

Using Flags with the ng generate command
Let’s generate an about component without a subfolder and without the specification file.

Head back to the terminal and run the following command:

$ ng generate component about --flat=true --spec=false


Using the angular.json File

Let’s now see by example how to configure the CLI from the angular.json file to not generate .spec.ts file for a component.

Go back to your terminal and run the following command:

$ ng config schematics.@schematics/angular:component.spec false


How to Set the Component’s Folder

You can aso define the folder of the component by specefying the path as follows:

$ ng generate component components/contact

This will create the contact component inside the components folder which will also be created if it doesn’t exist inside the src/app folder of the project.

Manually Creating Components

You can also create Angular components manually by creating the necessary files and add the component’s class to the declarations array of the module where it should be used.

Angular CLI Naming Conventions

When you create an Angular component with Angular CLI, it will follow these conventions:

The Component suffix is added to the name you submit for the component.
The app- prefix is added to the selector of the component.
The name of the component class is in upper camel case,

That’s what this article wants to share with you on how to create components in Angular using the ng generate component command or its ng shortcut shortcut command.

Learn More

Thanks for reading !

#angular #angular9 #webdeveloper

How to Create Components in Angular 9
2 Likes382.00 GEEK