Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new component in Angular 4 using CLI

Tags:

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

like image 949
surbhiGoel Avatar asked May 24 '17 07:05

surbhiGoel


People also ask

How do I create a component in Angular 4?

There are mainly two ways to generate new component in Angular, using ng g c <component_name> , another way is that using ng generate component <component_name> . using one of these commands new component can be generated. Show activity on this post. ng generate component componentName.

How many new files are created and updated when you create a new component using CLI?

CLI Component Generation It looks like the CLI automatically created the new folder to hold our new component in app/virtual-machines . In addition we see four new files associated with the automatically generated component.

What is CLI command in Angular?

The Angular CLI is a command-line interface tool that you use to initialize, develop, scaffold, and maintain Angular applications directly from a command shell.

What is the command to generate a new module?

ng g module NewModule to generate a module. ng g component new-module/new-component to create NewComponent.


1 Answers

In Angular4 this will work the same. 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

you can use ng --help, ng g --help or ng g c --help for the docs.

Ofcourse rename YOURCOMPONENTNAME to the name you would like to use.

Docs: angular-cli will add reference to components, directives and pipes automatically in the app.module.ts.

Update: This still functions in Angular version 8.

like image 95
Mike Bovenlander Avatar answered Oct 04 '22 03:10

Mike Bovenlander