Angular 9/8 How-To: Create Angular Components?
In this post we will give you information about Angular 9/8 How-To: Create Angular Components?. Hear we will give you detail about Angular 9/8 How-To: Create Angular Components?And how to use it also give you demo for it if it is necessary.
In this quick how-to post, we’ll learn how to use the ng generate component command of Angular 9 CLI, or its ng g c shortcut command, for creating Angular 9 components in your project.
If you are new to these how-tos, check out how to install and set up a project and the prerequisites.
How to Use ng generate component or ng g c with Angular 9 CLI
Let’s assume we want to create a Angular component named home.
Open a new command-line interface, navigate into the root of your Angular project and run the following command:
$ ng generate component homeOr you can also run the following command:
$ ng g c homeAngular CLI will generate 4 files for the component in the src/app folder of your project:
/home/home.component.ts # Component class /home/home.component.html # Component template /home/home.component.css # Component styles /home/home.component.spec.ts # Component testsWe 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,HomeComponent]// [...]})exportclassAppModule{}This will allow HomeComponent in any component in the AppModule.
How to Customize the Output of ng generate component in Angular 9
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.jsonconfiguration 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=falseUsing 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 falseHow 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/contactThis 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 Angular 9 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 9 CLI Naming Conventions
When you create an Angular component with Angular CLI, it will follow these conventions:
- The
Componentsuffix 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,
Conclusion
In this howto post, we have seen how to create components in Angular 9 using the ng generate component command or its ng g c shortcut command.
Hope this code and post will helped you for implement Angular 9/8 How-To: Create Angular Components?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
