user8520658
user8520658

Reputation:

How to generate components in a specific folder with Angular CLI?

I am using Angular 4 with Angular CLI and I am able to create a new component with the following command.

E:\HiddenWords>ng generate component plainsight

But I need to generate a child component inside plainsight. Is there a way to do it with Angular CLI?

Upvotes: 405

Views: 664622

Answers (24)

ihaveaquestion
ihaveaquestion

Reputation: 117

I know that you want to make a child component inside of an existing one, but that is no different to any other cli, like many others already commented. (ng g component <folder_name>/<component_name>)

Now to the question that got me here: "How to generate components in a specific folder with Angular CLI?"

I would define the generate location of components in the angular.json configuration, so that you do not have to remember anything and can not make grammer mistakes or spelling errors. This also speeds things up, because you do not have to repeat yourself over and over again.
I hope this helps someone, because I thought I would find out, how to do this, under this post and didn't find the solution in the docs.

This is what you have to add in projects.<project_name>.schematics:

{
        "@schematics/angular:component": {
          "path": "src/app/components",
          "style": "css"
        },
        "@schematics/angular:service": {
          "path": "src/app/services"
        },
        "@schematics/angular:interface": {
          "path": "src/app/types"
        },
        "@schematics/angular:enum": {
          "path": "src/app/types"
        }
        ...
}

Upvotes: 0

WasiF
WasiF

Reputation: 28909

There are couple of methods to create component in a specific directory.

Method 1: "Open in Integrated Terminal" - Quick, Simple and Error free method

i.e. You want to create a component in a app/common folder as shown in the image given below, then follow these steps

  1. Right click on the folder in which you want to create component.
  2. Select option Open in Integrated Terminal or Open in Command Prompt.
  3. In new terminal (you'll see your selected path), then type ng g c my-component

Also you can check this process through this image enter image description here

Method 2: "Copy Relative Path" and Paste on Terminal

  1. Right click on folder in which you want to create component
  2. From context menu, select Copy Relative Path
  3. On termincal, type cd, press space and then ctrl + v to paste the copied path and hit Enter
  4. You will see that directory has been changed.

Also you can check this process through this image enter image description here

Method 3: Type complete path on terminal

i.e. You want to create component in some folder, you type the whole command including path and component name.

ng g c relative-path/my-component

Also you can check this process through this image enter image description here

Note (method-3): angular will not allow you to create component outside app folder so for component, your base path will be app folder that's why in my case I had to start with auth/a-component instead of src/app/auth/a-component

Upvotes: 177

Affrudeen B
Affrudeen B

Reputation: 31

Open a terminal window and navigate to the root of your Angular project. create a folder

Run the following command to generate a new component in a specific folder:

ng generate component <folderName>/<componentName>

For example, if you want to create a new component called "myComponent" in a folder called "components", the command would be:

ng generate component components/myComponent

Upvotes: 3

Brian Akumah
Brian Akumah

Reputation: 304

Once you are in the directory of your project. use cd path/to/directory then use ng g c component_name it automates everything and is error free

the g c means generate component

Upvotes: 1

Wand Maker
Wand Maker

Reputation: 18762

Say you want to create a component Container in a module Box, and module is already created and is present in it its own folder src/app/box.

You can do this - when asked for component name, provide path to component and component name.

ng g c -m box
? What name would you like to use for the component? box/Container
CREATE src/app/box/container/container.component.scss (0 bytes)
CREATE src/app/box/container/container.component.html (24 bytes)
CREATE src/app/box/container/container.component.spec.ts (647 bytes)
CREATE src/app/box/container/container.component.ts (288 bytes)
UPDATE src/app/box/box.module.ts (286 bytes)

Upvotes: 0

Junaid khan
Junaid khan

Reputation: 142

Open the specific folder Terminal use this command

ng g c component name --flat

Upvotes: 2

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 19173

2021 Further speed up using IDE pluggins

If you want to further speed up this usual process and avoid common mistakes which cost you time you can try with visual studio code plugins.

For example I use Angular-Schematics and I avoid the terminal. You can invoke the ng cli with your mouse selections.

enter image description here

Upvotes: 5

Matthew
Matthew

Reputation: 309

Use 'ng generate component ./target_directory/Component_Name'

Upvotes: 11

Prageeth godage
Prageeth godage

Reputation: 4564

It Super Easy,

enter image description here

Other way,

in terminal go your FOLDER,

**xxx\FOLDER_NAME>cd FOLDER_NAME

xxx\FOLDER_NAME> ng generate component plainsight

Bonus point if you lasy like me to copy path(Visual studio code),

enter image description here

Upvotes: 10

syed
syed

Reputation: 636

To open a terminal in VS code just type CTRL + ~ which will open the terminal. Here are the steps:

  1. Check for the particular component where you need to generate the new component.

  2. Redirect the path to the particular folder/component where you need to generate another component

FOR EXAMPLE: cd src/app/particularComponent

In the place of particularComponent, type the component name in which you need to generate the new component.

  1. Once you are in the component where you need to generate the new component, just type this command: ng g c NewComponentName

(Change the name NewComponentName to your required component name.)

Upvotes: 5

Seshu Vuggina
Seshu Vuggina

Reputation: 821

Create a component inside a specific folder:

ng g c folder-name/component-name

Create a component inside a folder for a specific (existing) module with Angular-CLI:

ng g c folder-name/component-name --module=folder-name/moduleName.module.ts

Upvotes: 7

Ismail
Ismail

Reputation: 1316

Need for using --dryRun when using custom directory

You can pass your custom directory path along with ng command.

ng g c myfolder\mycomponent

But there are chances that you miss spell the path and either new folder gets created or target directory changes. For this reason dryRun is very helpful. It displays an output of how the changes is going to be affected.
enter image description here

After verifying the result you can run the same command without -d to make the changes.

--dryRun=true|false

When true, runs through and reports activity without writing out results.

Default: false

Aliases: -d

Official Doc :- https://angular.io/cli/generate

Upvotes: 3

Sof&#237;a
Sof&#237;a

Reputation: 914

The above options were not working for me because unlike creating a directory or file in the terminal, when the CLI generates a component, it adds the path src/app by default to the path you enter.

If I generate the component from my main app folder like so (WRONG WAY)

ng g c ./src/app/child/grandchild 

the component that was generated was this:

src/app/src/app/child/grandchild.component.ts

so I only had to type

ng g c child/grandchild 

Hopefully this helps someone

Upvotes: 15

C.OG
C.OG

Reputation: 6529

If you use VSCode, consider using the Angular Console

It provides an interface for the Angular CLI. You will see an option to specify the path.

The Angular CLI is immensely powerful and extensible. In fact, there are so many capabilities that it is helpful for developers to have all of the different configuration options for every command available to them.

With Angular Console, you’ll get recommendations and be able to pull up even the most easily forgotten or rarely used features!

Angular Console is, first and foremost, a more productive way to work with what the Angular CLI provides.

Upvotes: 0

datta ambareesh
datta ambareesh

Reputation: 161

Firstly to create a Component you need to use:-

  • ng g c componentname

    By using above command New Component will be created in a folder with
    (componentname) you specified above.

But if you need to create a component inside of another component or in specific folder:-

  • ng g c componentname/newComponentName

Upvotes: 0

Kavindu N
Kavindu N

Reputation: 383

Angular CLI provides all the commands you need in your app development. For your specific requirement, you can easily use ng g (ng generate) to get the work done.

ng g c directory/component-name will generate component-name component in the directory folder.

Following is a map of a few simple commands you can use in your application.

  1. ng g c comp-name or ng generate component comp-name to create a component with the name 'comp-name'
  2. ng g s serv-name or ng generate service serv-name to create a service with the name 'serv-name'
  3. ng g m mod-name or ng generate module mod-name to create a module with the name 'mod-name'
  4. ng g m mod-name --routing or ng generate module mod-name --routing to create a module with the name 'mod-name' with angular routing

Hope this helps!

Good Luck!

Upvotes: 3

Go to project folder in command prompt or in Project Terminal.

Run cmd : ng g c componentname

enter image description here

Upvotes: 1

esitarz
esitarz

Reputation: 385

I wasn't having any luck with the above answers (including --flat), but what worked for me was:

cd path/to/specific/directory

From there, I ran the ng g c mynewcomponent

Upvotes: 5

Erich Benevides Diniz
Erich Benevides Diniz

Reputation: 11

Try to use

ng g component plainsight/some-name.component.ts

Or try it manually, if you feel more comfortable.

Upvotes: 1

SACHIN DUHAN
SACHIN DUHAN

Reputation: 339

ng g c folderName/SubFolder/.../componentName --spec=false 

Upvotes: 6

Ahmed Adewale
Ahmed Adewale

Reputation: 3143

Simple

ng g component plainsight/some-name

It will create "plainsight" folder and generate some-name component inside it.

Upvotes: 1

VIKAS KOHLI
VIKAS KOHLI

Reputation: 8470

ng g c component-name

For specify custom location: ng g c specific-folder/component-name

here component-name will be created inside specific-folder.

Similarl approach can be used for generating other components like directive, pipe, service, class, guard, interface, enum, module, etc.

Upvotes: 37

user3611927
user3611927

Reputation: 3079

The ng g component plainsight/some-name makes a new directory when we use it.

The final output will be:

plainsight/some-name/some-name.component.ts

To avoid that, make use of the flat option ng g component plainsight/some-name --flat and it will generate the files without making a new folder

plainsight/some-name.component.ts

Upvotes: 307

Ace Valdez
Ace Valdez

Reputation: 261

more shorter code to generate component: ng g c component-name
to specify its location: ng g c specific-folder/component-name


Additional info
more shorter code to generate directive: ng g d directive-name
to specify its location: ng g d specific-folder/directive-name

Upvotes: 20

Related Questions