Angular 核心模块中的ng2翻译

Angular 核心模块中的ng2翻译,angular,angular-cli,ng2-translate,Angular,Angular Cli,Ng2 Translate,在我们的Angular应用程序中,我们有功能模块以及核心和共享模块,如 我们使用ng2 translate,根据doc,我们应该在App模块(root模块)中调用forRoot() 这就是我们的应用程序模块的外观: @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, BrowserAnimationsModule, FeatureAModule, Featu

在我们的Angular应用程序中,我们有功能模块以及核心共享模块,如

我们使用
ng2 translate
,根据doc,我们应该在App模块(root模块)中调用
forRoot()

这就是我们的应用程序模块的外观:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FeatureAModule,
    FeatureBModule,
    CoreModule,
    TranslateModule.forRoot({
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [Http]
    })
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
由于我们想要翻译菜单,并且它是核心模块的一部分,我们必须在那里导入翻译模块,如下所示:

@NgModule({
  imports: [
    TranslateModule
  ],
  exports: [
      FormsModule,
      MenuComponent,
      BreadcrumbComponent
  ],
  declarations: [MenuComponent, BreadcrumbComponent]
})
export class CoreModule {
    constructor( @Optional() @SkipSelf() parentModule: CoreModule) {
      throwIfAlreadyLoaded(parentModule, "CoreModule");
    }
 }

这有意义吗?我是否应该从应用程序模块中删除
TranslateModule.forRoot(…)
,并将其放置在核心模块的导入中?错了吗?

如果您遵循文档,那么
AppModule
将是唯一导入
CoreModule
的。如果是这种情况,只需将
TranslateModule.forRoot()
添加到
CoreModule.imports
array和
TranslateModule
添加到
CoreModule.exports
array,一切都会正常工作。然后在
AppModule
中,只需导入
CoreModule
,而无需再次处理translate模块

这与文档建议集成
RouterModule
的方式类似。坐一会儿。请注意,
RouterModule.forRoot()
是在
AppRoutingModule
中导入的,而不是在
AppModule
本身中导入的。因此,在你的位置上,我会:

核心模块

// forRoot is OK here because AppModule is the only one to import CoreModule
imports: [TranslateModule.forRoot(...)]

// will make Translate available to AppModule
exports: [TranslateModule]
//importing Core will import Translate and its services provided by .forRoot()
imports: [CoreModule]
//only if the components, directives and pipes of SharedModule need Translate
imports: [TranslateModule]

//so that all modules that import SharedModule will have Translate access
exports: [TranslateModule]
AppModule

// forRoot is OK here because AppModule is the only one to import CoreModule
imports: [TranslateModule.forRoot(...)]

// will make Translate available to AppModule
exports: [TranslateModule]
//importing Core will import Translate and its services provided by .forRoot()
imports: [CoreModule]
//only if the components, directives and pipes of SharedModule need Translate
imports: [TranslateModule]

//so that all modules that import SharedModule will have Translate access
exports: [TranslateModule]
共享模块

// forRoot is OK here because AppModule is the only one to import CoreModule
imports: [TranslateModule.forRoot(...)]

// will make Translate available to AppModule
exports: [TranslateModule]
//importing Core will import Translate and its services provided by .forRoot()
imports: [CoreModule]
//only if the components, directives and pipes of SharedModule need Translate
imports: [TranslateModule]

//so that all modules that import SharedModule will have Translate access
exports: [TranslateModule]

这是正确的方法。对于标准AngularCLI版本,需要编译到根模块的项目需要保留在应用程序模块中。核心模块的问题是什么?为什么我要在核心模块中复制它?即使它已编译到应用程序根目录中,您的核心模块也需要某种方式知道它将使用您的TranslateModule。TranslateModule将被编译到您的应用程序根目录中,导入它的所有其他模块将使用它的这一个实例。好的,根据ng2 translate的文档,实例共享模块应该只导出它,而不导入它。是因为核心模块是一个急切加载的模块吗?因为核心模块在AppModule中静态使用,所以需要导入。由于SharedModule已导入其他模块/组件,因此需要导出。请记住,应用程序根目录正在保留您的ng2 translate,因为它已经可用,所以您希望确保使用它的任何东西都知道它在哪里可用。Core不使用shared,因此需要直接导入。其他模块将使用shared,因此shared会将位置导出到其他模块。谢谢,这正是我使用的方法,但我不确定这是否是正确的方法。