How to Create Components in Angular 9

How to Create Components in Angular 9

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.

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 [email protected]/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 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Upgrade Angular 8/7 to Angular 9 With Ng Update & Angular CLI v9

Updating Angular CLI to v9 and upgrading your project to Angular 9 from v8/7 is now easier than ever before, thanks to all the work that has been done in version 9 and the ng update command which allows you to update specific versions and...

Angular: Novidades Angular v9 Ivy: TypeScript 3.7

Mais um vídeo da série sobre Angular v9 e Angular Ivy! Esse tutorial é uma dica rápida mostrando as duas maiores novidades to TypeScript 3.7 que é o operador de optional chaining e o operador de coalescência nula.

How to Use Cookies in Angular for Storing user’s Credentials

In this post, I will be explaining about using Cookies in Angular for Storing user’s Credentials