angular-cli

How to create a new component in Angular 4 using CLI


In angular 2 I use

ng g c componentname

But It is not supported in Angular 4, so I created it manually, but it shows error that it is not a module.


Solution

  • This still works the same in Angular 4+. If you get an error, I think your problem is somewhere else.

    In command prompt type:

    ng generate component YOURCOMPONENTNAME
    

    There are even shorthands for this: the commands generate can be used as g and component as c:

    ng g c YOURCOMPONENTNAME
    

    Of course, rename YOURCOMPONENTNAME to the name you would like to use.

    You can use ng --help, ng g --help or ng g c --help for help, or visit https://angular.dev/cli/generate/component.