First of all, it's not a duplicate of any other question and I've read the angular guide on that. However I still have several questions.
The feature module is the easiest one - you have a feature - group it into feature module. Let's say that in addition to obvious feature I have the pages which every application has:
I could probably move everything to feature module called 'static' but I don't like the name and also don't like grouping mostly unrelated things into the same module, i.e. error page and contact page. So, what is a pattern for mentioned pages?
Now, shared vs core module. I have the following items:
So, the main question is how to choose decide for the items I listed and for new items like those.
How do CoreModule and SharedModule fit into this? To keep it simple, the CoreModule is a Service module and the SharedModule is a Widget module. And that's why you should import the first only once in the AppModule and the latter in all modules that need it.
The Core Module is where we want to put our shared singleton services. So the services that we want only one instance of while having them shared among multiple modules should live here. The Angular injector creates a new instance of a service for each lazily loaded module it is provided.
The core module usually contains components that are used once in an Angular application, such as a navigation bar, loader, footer, etc. This module should be loaded globally in AppModule .
The answers to your question are subjective, however there are some recommendations from official docs you can follow: What kinds of modules should I have and how should I use them?. If you haven't read docs on NgModule
and FAQ, I'd suggest spending a few hours studying them, things will be much clearer (at least they were for me :)
I'm using the following setup and it works quite well for me:
directives
, components
and pipes
, just to keep things organized a little better. Examples: filesize.pipe
, click-outside.directive
, offline-status.component
...about.component
, contact.component
, app-toolbar.component
ui.service
, auth.service
, auth.guard
, data.service
, workers.service
....user-profile.component
, dashboard.component
, dashboard-sidebar.component
...player
, playlist
, favorites
submodules would go. If you look at the @angular/material2
this would be an equivalent to their MaterialModule
and many submodules, like MdIconModule
, MdSidenavModule
etc. General advice would be:
To answer your specific questions: I would put all those routes in one module - static
, public
, whatever the name. CsrfService
- core, Logger
- core or dev, HttpModule
- core, you only need one instance (probably), auth
- core. Don't put services in shared.
If you can't decide how/what to group in a feature, make a new app, copy specific feature folder and it should work there as well. If it doesn't, you'll need to organize things better.
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