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.
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.