Creating a Component using Angular CLI
Introduction
Angular CLI is a command line interface for the Angular framework and is the recommended way to create components. Components are the fundamental building blocks of an Angular application, and this guide will explain how to use the Angular CLI to create components and use them in an application.
Steps for creating a Component
Step 1: Install the Angular CLI
The first step is to install the Angular CLI if you haven’t already. To do this, open up a terminal window and run the following command:
npm install -g @angular/cli
Step 2: Create a New Project
Now that the CLI is installed, the next step is to create a new project. To do this, run the following command in the terminal window:
ng new my-project
This will create a new project in a folder called “my-project”.
Step 3: Create a Component
Now that the project is created, the next step is to create a component. To do this, run the following command in the terminal window:
ng generate component my-component
This will create a new component in a folder called “my-component”.
Step 4: Import the Component
The next step is to import the component into the application. To do this, open the `app.module.ts` file and add the following line at the top:
import { MyComponentComponent } from './my-component/my-component.component'
Then add the component to the `declarations` array in the `@NgModule` decorator:
@NgModule({
declarations: [
MyComponentComponent
]
})
Step 5: Use the Component
Now that the component is imported, the last step is to use it in the application. To do this, open the `app.component.html` file and add the following line:
This will add the component to the application.
Conclusion
This guide has shown how to create a component using the Angular CLI and use it in an application. By following the steps outlined above, you can quickly create components and add them to your application.