Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 1072 of the free egghead.io lessons, plus get Angular 2 content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Generate components with the Angular CLI

1:21 Angular 2 lesson by

Generating components with the Angular CLI allows for scaffolding applications quickly. Learn what will be generated for you and how to generate quickly with shortcuts in the desired directory.


egghead.io comment guidelines

Avatar
egghead.io

Generating components with the Angular CLI allows for scaffolding applications quickly. Learn what will be generated for you and how to generate quickly with shortcuts in the desired directory.

Type "ng generate component" to indicate that you want to generate a component. Then specify the name of the component. In this case I'll use "people" as the name of the component. In the code editor we see our new people folder that contains an empty style file, a simple HTML template file, the TypeScript component file, and a test file.

Also notice that there have been five file changes made -- the four new files, as well as a change to the app.module file. The people component has been imported and declared with an app.module.

As a shortcut, one generating component instead of typing out "ng generate component," there is a shorthand to type "ng," "g" for generate, "c" for component, and then the name of the component. These two statements will yield the same results.

Type "ng g c people" to indicate the people folder and "person" as the name of the new component you wish to generate. As you can see by the generated output, the person component has been created underneath the people folder.

The path you specify for generated and component is relative to the directory in which you are creating it. To create an address component underneath person, navigate it to the person directory which was just created. Once there, you can generate the address component.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?