1 | initial version |
To create a child component with Angular CLI in Angular, follow these steps:
Open your terminal and navigate to the root of your project.
Run the command ng generate component <component-name>
where <component-name>
is the name of your child component.
This will create a new folder in your src/app
directory with the name you specified, containing the files for the new child component.
In the parent component where you want to use the child component, import the child component by adding import { <component-name> } from './<component-name>/<component-name>.component'
at the top of the parent component's .ts
file.
Add the child component to the parent component's @NgModule
declaration by adding <component-name>
to the declarations
array.
In the parent template where you want to use the child component, insert the child component tag by adding <app-<component-name></app-<component-name>>
to the parent component's HTML file.
Save your changes and run your Angular application. Your child component should now be visible within the parent component.