So, have you ever gotten tired of generating components one at a time?
Yeah, me too. Thatβs why I started looking for smarter ways to automate this process β and now Iβm here to share them with you.
π Introduction
When developing scalable Angular applications, keeping your components well-structured and organized is crucial.
Instead of generating each one manually using ng generate component, letβs level up and make the process faster, cleaner, and more productive.
π Step 1 β Understanding the Basics
β What is a Component?
In Angular, a component is a core building block of the user interface. It includes:
- ts (logic)
- html (template/view)
- css/.scss (style)
- spec.ts (unit test β optional)
β How to Create a Component (manually)
ng generate component components/header
Or simpply:
ng g c components/header
Which generates:
src/app/components/header/
βββ header.component.ts
βββ header.component.html
βββ header.component.scss
βββ header.component.spec.ts
π§° Step 2 β Creating Multiple Components at Once (Intermediate Method)
π¦ Option 1: One-liner with a loop (Git Bash, Linux, or Mac)
for c in header sidebar footer dashboard; do ng g c components/$c; done
This command will quickly generate multiple components inside the components/ folder:
- header
- sidebar
- footer
- dashboard
π¦ Option 2: Using && to chain commands
ng g c components/register &&
ng g c components/list &&
ng g c components/edit &&
ng g c components/delete
This creates the same result in a straightforward way β great for quick setups.
π¦ Option 3: Creating a reusable script in package.json
You can add a script to automate component generation:
{
"scripts": {
"component-generator": "ng g c components/register && ng g c components/list && ng g c components/edit && ng g c components/delete"
}
}
Then just run:
npm run component-generater
β¨ Step 3 β Advanced Tips
π Add extra options to customize component creation:
Option Description
--skip-tests Does not create the test file
--style=scss Uses SCSS instead of CSS
--flat Creates the component without a folder (saves directly in components/)
--standalone Creates a standalone component (Angular 14+)
Exemplo:
ng g c components/navbar --skip-tests --style=scss --flat
β Expected Final Result
By the end of this guide, youβll have multiple components quickly generated, organized, and ready to use β following solid practices for modularization and automation in Angular.
Thanks so much for reading! I hope this guide saves you time (and sanity π ).
If you enjoyed this content, leave a β€οΈ, Save it for later, and Share it with your dev crew!
And if you want to add something, drop a comment and weβll learn with your contribuition and go to next level. π
βοΈ Written by: Nelson Manuel Garcia
Feel free to reach out if you have questions or want to connect! π§π»
Top comments (0)