I have read this explanation from the official angular blog https://blog.angular.io/schematics-an-introduction-dc1dfbc2a2b2
Schematics is a workflow tool for the modern web; it can apply transforms to your project, such as create a new component, or updating your code to fix breaking changes in a dependency. Or maybe you want to add a new configuration option or framework to an existing project.
and I even followed the example in the blog of how to generate a new schematic
npm install -g @angular-devkit/schematics-cli
schematics blank --name=my-component
after which I opened the my-component folder. This is where my confusion starts... The generated files could be generated by
ng new
the only difference being the above command will produce more files than the schematics one. From the description: the creating of new components and updating your code done by schematic but this can be done by the angular CLI by using "ng new"
The Angular CLI is using schematics under the hood. When you're happy with the Angular CLI for creating projects and components, you don't need to know anything of schematics. When you want another setup AND want other developers to use your setup you could create your own schematics.
For example: we are creating a large application with over 150 different pages with multiple devOps teams. We have created schematic to simply create pages, features and use services which are fit in our application setup. Making a schematic is much easier to maintain then explaining to every developer how to setup a page. It also brings consistency in the project.
Other big projects like Ionic or NX https://nrwl.io/nx also make use of schematics to let you create application and pages within their architecture.
So for your answers:
The advantage of using schematics: With the schematic, you can automate creating boilerplate code, and you can 'document' the best practice.
You could setup how you want your application/pages etc to be created instead of how Angular thinks what is the best way.
You could do everything without schematics. either by hand or creating your own NodeJS scripts. Schematics just helps you creating the scripts and provides you with an interface and the possibility to do a dry run.
Check out this tutorial: https://medium.com/rocket-fuel/angular-schematics-simple-schematic-76be2aa72850.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With