How to create a new component in Angular 4 using CLI

Viewed 433408

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.

25 Answers

Generating and serving an Angular project via a development server -

First go to your Project Directory and the type the below -

ng new my-app
cd my-app
ng generate component User (or) ng g c user 
ng serve

Here “D:\Angular\my-app>” is my Angular application Project Directory and the “User” is the name of component.

The commonds looks like -

D:\Angular\my-app>ng g component User
  create src/app/user/user.component.html (23 bytes)
  create src/app/user/user.component.spec.ts (614 bytes)
  create src/app/user/user.component.ts (261 bytes)
  create src/app/user/user.component.css (0 bytes)
  update src/app/app.module.ts (586 bytes)

1) first go to your Project Directory

2) then Run below Command in terminal.

ng generate component componentname

OR

ng g component componentname

3) after that you will see output like this,

create src/app/test/componentname.component.css (0 bytes)
create src/app/test/componentname.component.html (23 bytes)
create src/app/test/componentname.component.spec.ts (614 bytes)
create src/app/test/componentname.component.ts (261 bytes)
update src/app/app.module.ts (1087 bytes)

If you want to create new component without .spec file, you can use

ng g c component-name --spec false

You can find these options using ng g c --help

For creating component under certain folder

ng g c employee/create-employee --skipTests=false --flat=true

This line will create a folder name 'employee', underneath that it creates a 'create-employee' component.

without creating any folder

ng g c create-employee --skipTests=false --flat=true

Make sure you are in your project directory in the CMD line

    ng g component componentName
ng g c componentname

It will create the following 4 files:

  1. componentname.css
  2. componentname.html
  3. componentname.spec.ts
  4. componentname.ts
ng g c COMPONENTNAME

this command use for generating component using terminal this i use in angular2.

g for generate c for component

ng g component component name

before you type this commend check you will be in your project directive path

go to your project directory in CMD, and run the following commands. You can use the visual studio code terminal also.

ng generate component "component_name"

OR

ng g c "component_name"

a new folder with "component_name" will be created

component_name/component_name.component.html component_name/component_name.component.spec.ts component_name/component_name.component.ts component_name/component_name.component.css

new component will be Automatically added module.

you can avoid creating spec file by following command

ng g c "component_name" --nospec

ng g c COMPONENTNAME should work, if your are getting module not found exception then try these things-

  1. Check your project directory.
  2. If you are using visual studio code then reload it or reopen your project in it.

Go to Project location in Specified Folder

C:\Angular6\sample>

Here you can type the command

C:\Angular6\sample>ng g c users

Here g means generate , c means component ,users is the component name

it will generate the 4 files

users.component.html,
users.component.spec.ts,
users.component.ts,
users.component.css

Step1:

Get into Project Directory!(cd into created app).

Step2:

Type in the following command and run!

ng generate component [componentname]

  • Add the name of component you want to generate in the [componentname] section.

OR

ng generate c [componentname]

  • Add name of component you want to generate in the [componentname] section.

Both will work

For reference checkout this section of Angular Documentation!

https://angular.io/tutorial/toh-pt1

For reference also checkout the link to Angular Cli on github!

https://github.com/angular/angular-cli/wiki/generate-component

ng generate component componentName.

It will automatically import the component in module.ts

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.

cd to your project and run,

> ng generate component "componentname"
ex: ng generate component shopping-cart

OR

> ng g c shopping-cart

(This will create new folder under "app" folder with name "shopping-cart" and create .html,.ts, .css. specs files.)

If you do not want to generate .spec file

> ng g c shopping-cart --skipTests true

If you want to create component in any specific folder under "app"

> ng g c ecommerce/shopping-cart

(you will get folder structure "app/ecommerce/shopping-cart")

I think you have to upgrade the latest angular cli using this command.

npm install -g @angular/cli

Then if you run ng g c componentName, it will work.

if you want that component not appears in one folder.

ng g c NameComponent --flat
Related