In Angular, you can create a new component using the Angular CLI. The following steps outline the process:
- Open the terminal and navigate to your Angular project directory.
- Run the following command to generate a new component:
ng generate component <component-name>
Replace <component-name>
with the name of your component. For example, if you want to create a component named “header“, run the following command:
ng generate component header
- The Angular CLI will generate the following files for the new component:
.ts
file for the component class.html
file for the component template.css
or.scss
file for the component styles.spec.ts
file for unit tests
- Add the component to the module: To use the new component in your application, you need to add it to the module. Open the
app.module.ts
file and import the component:
import { HeaderComponent } from './header/header.component';
Then, add the component to the declarations
array:
declarations: [
HeaderComponent
]
- Use the component in your template: To use the new component in your template, you can add it as a tag. For example, if you want to use the “header” component, you can add the following tag to your template:
<app-header></app-header>
That’s it! Your new component is now ready to use in your Angular application.