Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the path of component generation to current path in angular-cli?

I'm following an angular 2 course where the instructor navigates into the specific path where he wants to generate a component and then runs the following command:

ng generate component component-name

After he runs it, the tool generates the files inside the current directory, but when I try the same, the tool ends up generating the files at the app root folder.

In other words, if I do this:

app\my-component> ng generate component component-name

I expect the command to generate files here:

app\my-component\component-name\
  component-name.component.html
  component-name.component.ts
  ...

But instead is doing it here:

app\component-name\
  component-name.component.html
  component-name.component.ts
  ...

How can I tell ng-cli to use current path?

like image 820
jacoviza Avatar asked Feb 09 '17 19:02

jacoviza


People also ask

Which command will you run in the angular CLI to generate a component?

Creating a component using the Angular CLIlink Run the ng generate component <component-name> command, where <component-name> is the name of your new component.


3 Answers

ng g c 'path/to/component/componentName'

You should never have to leave your root directory when working with angular cli. If you wanted all your components to go into a subdirectory called my-component you would just do ng g c my-component/componentName

Note that g and c are valid shortforms for generate and component respectively.

like image 86
Jesse Carter Avatar answered Oct 13 '22 03:10

Jesse Carter


Suppose you have structure src/app and you wanna create a component header living at src/app/components/ you can run ng g c components/header, which will generate files at src/app/components/header folder, like src/app/components/header/header.component.ts src/app/components/header/header.component.html etc.

like image 26
xwa130 Avatar answered Oct 13 '22 01:10

xwa130


Well, after some googling and reading the other answers, it will be helpful to have some explanations here. To generate a component in your project:

cd  path/your-project

In the console (node-prompt or your IDE console) type:

ng generate component components/your-new-component-name

If you want to abbreviate:

ng g c components/your-new-component-name

You can create other angular components like: ng g directive ng g module ...

Source and more info: Angular Official Documentation

You should avoid: 'route-to-your-project': this will concatenate routes when you execute: ng g c 'route-to-your-project' and the console will show you an error because could not find the correct place to generate.

Example:

 ng g c components/alerts/test3

CREATE src/app/components/alerts/test3/test3.component.html (20 bytes)
CREATE src/app/components/alerts/test3/test3.component.spec.ts (621 bytes)
CREATE src/app/components/alerts/test3/test3.component.ts (266 bytes)
CREATE src/app/components/alerts/test3/test3.component.scss (0 bytes)
UPDATE src/app/components/alerts/alerts.module.ts (5046 bytes)

I hope it will be helpful for you

like image 35
Skatt Avatar answered Oct 13 '22 02:10

Skatt