Why is it must to import SharedModule to Root Module?
Angular 2 document states “Shared features modules must be imported by any module expecting to use it’s declarables”.
I wonder why is it must to import Shared features module in to root module. Does it increase code base complexity?
Modules are a way to declare stuff in angular, it defines what is needed by this logical block and what is exported by it. If you don’t import your
SharedModule in your
RootModule, the components/services/pipes/etc defined in this
SharedModule won’t be available. For example, if you want to be able to use some built-in directives (
ngFor,etc…), you need to import
Let’s say your
SharedModule defines a component:
awesome as selector. If you want to be able to use this component in your
RootModule (and why wouldn’t you, it is awesome ?), you need to import the module that declares it, otherwise angular will complain (or fail silently) that it doesn’t know this
Declaring multiple Modules can be useful if you want to isolate stuff, it enables you to make standalone library/part of application that you can reuse. If you make a new application and realize that awesome component you developped weeks ago would perfectly fit in it, if it has its own module you just need to import it.
It also allows easier bundling. instead of including that
SharedModule in every lazy-loaded modules (lazy-loaded routes requires separate modules), you bundle it /load it once for all.
So does it increase code-complexity? yes, a bit, but it has many advantages.
Answered By – n00dl3