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.
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.
We can use ng generate component command to create component in sub-directory or specific folder.
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.
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
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).
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With