I read an article about organizing folders in Angular and the author uses one called core
, in which he creates the core module and insert only services which will be called once.
Currently, I'm working on a project whereupon I adopted the structure recommended by the article. Inside my core
folder/module I created a module called api
:
\app
\core
\api
...
\shared
...
My question is: as the api
is a module, I was wondering why not remove it from core
and put in app
folder?
\app
\api
\core
\shared
...
Angular itself has core
module too (where we import ngModule
, Injectable
...) and I'd like to know what is its role.
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 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.
Modules in angular are a great way to share and reuse code across your application. This is an affiliate link. We may receive a commission for purchases made through this link. Shared modules do not only make your app tidier, but can reduce the actual size of an application by far.
CoreModule should have only services and be imported only once in the AppModule . SharedModule should have anything but services and be imported in all modules that need the shared stuff (which could also be the AppModule ).
Your CoreModule contains code that will be used to instantiate your app and load some core functionality.
To get more idea on this read Core Module
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