In our Angular App we have Feature Modules as well as Core and Shared Module as described by the Angular - linkStyle Structure best practices.
We use ng2-translate and as per doc, we should call forRoot() in App Module (root module).
This is how our App Module looks like:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FeatureAModule,
FeatureBModule,
CoreModule,
TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [Http]
})
],
bootstrap: [AppComponent]
})
export class AppModule { }
As we want to translate our menu and it's part of the Core Module we had to import the Translate Module there, like so:
@NgModule({
imports: [
TranslateModule
],
exports: [
FormsModule,
MenuComponent,
BreadcrumbComponent
],
declarations: [MenuComponent, BreadcrumbComponent]
})
export class CoreModule {
constructor( @Optional() @SkipSelf() parentModule: CoreModule) {
throwIfAlreadyLoaded(parentModule, "CoreModule");
}
}
Does this make sense? Should I remove TranslateModule.forRoot(...) from the App Module and place it in the imports of the Core Module ? Is that wrong?
If you're following the docs, then
AppModulewill be the only one to importCoreModule. If that's the case, things would work just fine if you simply addTranslateModule.forRoot()toCoreModule.importsarray andTranslateModuletoCoreModule.exportsarray. Then in yourAppModule, all you need to do is import theCoreModulewithout having to deal with the translate module again.This is similar to how the docs suggest integrating the
RouterModulefor instance. Take a look at this. Notice thatRouterModule.forRoot()is imported inAppRoutingModule, but not inAppModuleitself. So in your place I would have:CoreModule
AppModule
SharedModule