How to create components inside a specific folder with Angular 9 CLI ?
This tutorial guides you on how to create components inside a specific folder with Angular 9 CLI.
Create components inside a specific folder with Angular 9 CLI
The following command will generate all classes for your new component including .spect.ts test files.
Generate new component and its classes
ng generate component <component-name> [options] (OR) ng g c <component-name> [options]
Let’s say your current project structure looks like below. Where you have recipes component and its classes. Note, I have skipped .spec.ts test file generation. To skip .spec.ts test files you just need to pass option “–skipTests true”
πsrc | β π app | β | β π recipes | β | β | β π recipes.component.html | β | β | β π recipes.component.ts | β | β | β π recipes.component.css | β | β π app.component.html | β | β π app.component.ts | β | β π app.component.css | β | β π app.component.spec.ts
Now, you wanted to generate new components recipe-detail, recipe list inside recipes folder and recipe-item component inside recipe-list folder as shown below.
πsrc | β π app | β | β π recipes | β | β | β π recipe-detail | β | β | β | β π recipe-detail.component.html | β | β | β | β π recipe-detail.component.ts | β | β | β | β π recipe-detail.component.css | β | β | β π recipe-list | β | β | β | β π recipe-item | β | β | β | β | β π recipe-item.component.html | β | β | β | β | β π recipe-item.component.ts | β | β | β | β | β π recipe-item.component.css | β | β | β | β π recipe-list.component.html | β | β | β | β π recipe-list.component.ts | β | β | β | β π recipe-list.component.css | β | β | β π recipes.component.html | β | β | β π recipes.component.ts | β | β | β π recipes.component.css | β | β π app.component.html | β | β π app.component.ts | β | β π app.component.css | β | β π app.component.spec.ts
Before Angular 9 version you need to follow the below steps to create components inside a specific folder.
- First, you need to generate a module or component using the commandΒ ng g module <module-name> orΒ ng g component<component-name>
- Change directory in to the new module or new component folderΒ cd <module-name> orΒ cd <component-name>
- Finally, create component as a child of that parent module or component using commandΒ ng g component <component-name>
Angular 9: Create components inside a specific folder
After Angular 9 you are not required to change directory in order to create components inside a specific folder. You just need to follow below steps.
- Let’s say you have created recipesΒ component (current project structure).
- To create components just use the path information in the angular CLI command as shown below. For example, ng g c <module-name or component-name>/ <new-component-name>Β .
The following angular CLI commands createsΒ new components recipe-detail, recipe list inside recipes folder and recipe-item component inside recipe-list folder as shown below.
> ng g c recipes/recipe-detail --skipTests true CREATE src/app/recipes/recipe-detail/recipe-detail.component.html (28 bytes) CREATE src/app/recipes/recipe-detail/recipe-detail.component.ts (302 bytes) CREATE src/app/recipes/recipe-detail/recipe-detail.component.css (0 bytes) UPDATE src/app/app.module.ts (784 bytes) > ng g c recipes/recipe-list --skipTests true CREATE src/app/recipes/recipe-list/recipe-list.component.html (26 bytes) CREATE src/app/recipes/recipe-list/recipe-list.component.ts (294 bytes) CREATE src/app/recipes/recipe-list/recipe-list.component.css (0 bytes) UPDATE src/app/app.module.ts (668 bytes) > ng g c recipes/recipe-list/recipe-item --skipTests true CREATE src/app/recipes/recipe-list/recipe-item/recipe-item.component.html (26 bytes) CREATE src/app/recipes/recipe-list/recipe-item/recipe-item.component.ts (294 bytes) CREATE src/app/recipes/recipe-list/recipe-item/recipe-item.component.css (0 bytes) UPDATE src/app/app.module.ts (680 bytes)
That’s it. You have learnt how to create components inside a specific folder with angular CLI in Angular 9.
Hope it helped π
Also See:
- Get index of ngFor element using index as value in attribute
- Set default value in the dropdown list in Angular 9
- Best way to delete components in Angular 9 with CLI ?
- HTML Property Binding in Angular : Data Binding
- This version of CLI is only compatible with Angular versions
- Global Angular CLI version is greater than your local version
- Create custom events and fire in Angular 9 with EventEmitter β Example
- ERROR in multi /bootstrap.min.css ./src/styles.css in Angular 9
- Bind selected element from drop down to an object in Angular 9
- Angular 9 Error : βapp-headerβ is not a known element
- Create common app-header component in Angular 9
- How to stop generation of .spec.ts test files using Angular CLI ?
- Add a Responsive Bootstrap Navigation Bar in Angular 9