I am going through the example of Angular2 Material and I see that all Material modules are imported in the root module using the forRoot()
method. So in my app I do the same.
Now I need to use some Material components within other Shared modules, which means that I need to import the related Material packages in my Shared module. I am not clear whether I need to use the forRoot()
method also while importing them in the Shared module.
Thanks in advance for any help
The forRoot static method is the method that configures the root routing module for your app. When you call RouterModule. forRoot(routes) , you are asking Angular to instantiate an instance of the Router class globally.
The forRoot() method creates an NgModule that contains all the directives, the given routes, and the Router service itself. The forChild() method creates an NgModule that contains all the directives and the given routes, but does not include the Router service.
imports: Other modules whose exported classes are needed by component templates declared in this NgModule. providers: Creators of services that this NgModule contributes to the global collection of services; they become accessible in all parts of the app.
Creating shared modules allows you to organize and streamline your code. You can put commonly used directives, pipes, and components into one module and then import just that module wherever you need it in other parts of your app.
forRoot
is only used for the main app module. It is a convention used so that only the app module gets application/singleton providers. This is to avoid providers that are supposed to be singletons, being created more than once for the application. For example
import { ModuleWithProviders } '@angular/core';
@NgModule({
declarations: [ SomeDirective ],
exports: [ SomeDirective ]
})
export class SharedModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: [ AuthProvider ]
}
}
}
Here we should only call forRoot
while importing into the app module, so that it can create the AuthProvider
only once as a singleton. All other modules that need the SharedModule
should simply import ShareModule
so that it can use the SharedDirective
.
So calling forRoot
in the app module gets you everything provided by that module (and conventionally the providers that come with calling forRoot
) into the app module. So all the components declared in your app module have access to to everything from that module.
But everything in the declarations
(this include components, directives, and pipes) isn't inherited by any sub-modules. So we still need to import the module into any other module we need it in.
Your question seems to be specifically about your ShareModule
. For this module, you should not use forRoot
, for reasons I mentioned above. You should just exports
the MD module(s). You only use imports
if some component declared in that SharedModule
actually requires any MD modules. For example, if you have a component that uses the MD button, and that component is a shared component that you declare in the SharedModule
. In this case you should imports
and exports
. But if there are no such components, you only need to exports
. This provides the MD module(s) to whatever module you import the SharedModule
into.
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