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?
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 gc my-component/componentName
Note that g and c are valid shortforms for generate
and component
respectively.
Suppose you have structure src/app
and you wanna create a component header
living at src/app/components/
you can run ng gc 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.
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 gc '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
I ran into something similar to this when following Max Schwartzmuller's Udemy course. I was trying to generate components in a subfolder parallel to app. The cli will create anything called that way directly under app.
c:\ng2\aufbs\src\mycomponents>ng g c my-test
installing component
create src\app\my-test\my-test.component.css
create src\app\my-test\my-test.component.html
create src\app\my-test\my-test.component.spec.ts
create src\app\my-test\my-test.component.ts
moving /mycomponents under app everything worked as expected.
set the path from app folder, so you dont need use ng gc 'C:\someFolder\your-project\src\app\some-folder\other-folder\your-component'
. just use ng gc '.\pages\shered\reset-password'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.