If we look at our project, we can see that Angular-CLI has automatically scaffolded out a my-button directory that contains stubs for all of the files that make up our button component. This includes its class boilerplate, my-button.component.ts, its template, my-button.component.html, its style sheet, my-button.component.ts, and its test file, my-button.component.spec.ts:
These stub files are very generic, but they cover virtually all the boilerplate code involved in creating a new component in Angular. Another detail handled for us is the actual importing of the component into our application through app.module.ts:
...
import { MyButtonComponent } from './my-button/my-button.component';
@NgModule({
...
declarations: [
AppComponent,
MyButtonComponent
],
...
})
export class AppModule { }
The module declarations section here has automatically been updated for us by the generate command, so all that we need to do is invoke the component within a template of our application:
<app-my-button></app-my-button>
If we look back at our application, we will see the words my-button works printed out from wherever in our app we have invoked the component.
Generating content into specific locations: Angular-CLI's generate command can accept a file path along with a name in order to control where your content will be generated. For instance, if I had an admin module in my Angular application where I organized all the functionalities that are unique to my administrator user experience, I might want to nest an admin unique component there with ng g component admin/dashboard. This will create all my component files in a directory under src/app/admin/dashboard instead of in my root module space.
We can create many types of content using the generate command. The syntax for scaffolding out a new piece of content is always the same, as follows:
ng generate <blueprint> <name>
You can find a complete list of all the available blueprints and examples of their usage in the following table:
Blueprint |
Angular-CLI |
Component |
ng generate component my-new-component |
Directive |
ng generate directive my-new-directive |
Pipe |
ng generate pipe my-new-pipe |
Service |
ng generate service my-new-service |
Class |
ng generate class my-new-class |
Guard |
ng generate guard my-new-guard |
Interface |
ng generate interface my-new-interface |
Enum |
ng generate enum my-new-enum |
Module |
ng generate module my-module |
Many of these generated blueprint types create simple boilerplate files in the root of our app module directory. However, Component, Directive, Pipe, Service, and Guard also scaffold out additional useful files.
Angular-CLI add-ons: The future of Angular-CLI and its convention-oriented approach to Angular development is evolving very rapidly. One feature on the future roadmap for Angular-CLI that will greatly shape the usage of it by the community is add-ons. Add-ons will allow developers to plug in functionality to Angular-CLI through shared modules to enhance their Angular applications. For example, custom blueprints for Angular-CLI could be created through an add-on, and allow developers to scaffold out custom content using the generate command.