Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate inside module folder

I'm working with an Angular 2 application based on Angular-CLI skeleton, I had the habit of structuring my src folder with one directory by module, and each component related to this module in the same folder.

src     app         documents             document-list.component.ts             document-list.component.html             documents.component.ts             documents.component.html             document.module.ts         app.component.ts         app.module.ts     main.ts  

Now that I'm using Angular-CLI, I would like to take profit of each feature, but when I'm generating a new component, no matter what, it keep creating a new folder for my component when I just want to put it inside the related folder.

src     app         documents             document-list                 document-list.component.ts                 document-list.component.html             documents.component.ts             documents.component.html             document.module.ts         app.component.ts         app.module.ts     main.ts  

Is it possible to keep my previous structure, is it a bad practice? The guideline recommends to avoid useless directory depth so I'm kind of disturbed.

like image 777
LoïcR Avatar asked Dec 23 '16 13:12

LoïcR


People also ask

Can we create module inside module?

yes you can use modules and subModules and i advice you to use Lazy Loading pattern, when your application will be bigger and bigger you will have a big performance problems because of the bundle size and the time it takes for loading.

Does Ng generate component create folder?

We can use ng generate component command to create component in sub-directory or specific folder.

Can we create module inside module in angular?

Approach: Create an Angular app that to be used. Create the navigation links inside the app component and then provide the “routerLink” directive to each route and pass the route value to the “routerLink” directive. Then add the routes to the routing.


2 Answers

Assuming you are in your application's root directory and want to create components inside src/app/documents as you show above, you can use the --flat option to have your component created without creating a directory. To create the component as you request above, use this command:

ng generate component document-list --flat 
like image 69
Brocco Avatar answered Nov 13 '22 10:11

Brocco


Using angular-cli you can run ng g component documents/document-list and it will create the structure you need, (assuming app is the root of your application).

enter image description here

Hope this helps.

like image 43
Gman Avatar answered Nov 13 '22 08:11

Gman