如何在功能模块层次结构中使用.forRoot()

ggabor:

谁能告诉我如何使用.forRoot()调用构造多个嵌套的功能模块层次结构?

例如,如果我有这样的模块怎么办:

- MainModule
- SharedModule
- FeatureModuleA
    - FeatureModuleA1
    - FeatureModuleA2
- FeatureModuleB

所有功能模块都具有.forRoot()静态函数。

如何以某种方式“转移” .forRoot()函数来定义FeatureModuleA

@NgModule({ 
  imports: [
    //- I can use .forRoot() calls here but this module not the root module
    //- I don't need to import sub-modules here, FeatureA only a wrapper
    //FeatureModuleA1.forRoot(), //WRONG!
    //FeatureModuleA2.forRoot(), //WRONG!
  ],
  exports: [
    //I cannot use .forRoot() calls here
    FeatureModuleA1, 
    FeatureModuleA2 
  ]
})
class FeatureModuleA {
  static forRoot(): ModuleWithProviders {
    return {
      //At this point I can set any other class than FeatureModuleA for root
      //So lets create a FeatureRootModuleA class: see below!
      ngModule: FeatureModuleA //should be: FeatureRootModuleA 
    };
  }
}

我可以为根使用创建另一个类,然后在FeatureModuleA的forRoot()函数中进行设置:

@NgModule({
  imports: [
    //Still don't need any sub module within this feature module
  ]
  exports: [
    //Still cannot use .forRoot() calls but still need to export them for root module too:
    FeatureModuleA1, 
    FeatureModuleA2 
  ]
})
class FeatureRootModuleA { }

但是,如何在此特殊的ModuleClass中“转移” .forRoot()调用?

如我所见,我需要将所有子模块直接导入到我的根MainModule中,并为每个子模块调用.forRoot():

@NgModule({
  imports: [
    FeatureModuleA1.forRoot(),
    FeatureModuleA2.forRoot(),
    FeatureModuleA.forRoot(),
    SharedModule.forRoot()
  ]
})
class MainModule { }

我对吗?在回答之前,请先查看以下文件:https : //github.com/angular/material2/blob/master/src/lib/module.ts

据我所知,这个仓库由官方的角度小组维护。因此,他们只需在特殊的MaterialRootModule模块中导入所有.forRoot()调用即可解决上述问题。我不太了解如何将其应用于我自己的根模块?什么是.forRoot的真正含义吗?这是相对于软件包而不是实际的Web项目吗?

保罗·萨姆索塔(Paul Samsotha):

通常forRoot用于添加应用程序/单个服务。

@NgModule({
  providers: [ /* DONT ADD HERE */ ]
})
class SharedModule {
  static forRoot() {
    return {
      ngModule: SharedModule,
      providers: [ AuthService ]
    }
  }
}

其理由是,如果添加AuthServiceproviders@NgModule,有可能为一个以上的被创建,如果您导入SharedModule到其他模块。

当将SharedModule导入到一个急切加载的模块中时,我不是100%清楚是否会创建该服务,但是提到的文档的解释是关于延迟加载的模块的。延迟加载模块时,将创建所有提供程序。

出于这个原因,我们添加了一个(按惯例)forRoot方法,以表示该方法仅应在根(应用)模块中调用,而在其他模块中,应仅正常导入该方法。

@NgModule({
  imports: [SharedModule]
})
class FeatureModule {}

@NgModule({
  imports: [SharedModule.forRoot()]
})
class AppModule {}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用forRoot传递配置数据

使用forRoot()导入模块

如何在Angular2编译器/ cli ngc命令中使用RouterModule.forRoot()

使用forRoot在哪里导入(导出?)模块?

为什么从Material2中删除了forRoot(),对于延迟加载的功能模块我该怎么办?

为什么在导入NgModule时必须使用AlertModule.forRoot()?

Angular 4 ReactiveFormsModule不应该使用forRoot模式吗?

Angular模块的forRoot中的运行时值

使用AOT时,注入时将放弃对传递给forRoot的对象所做的更改

NgModule嵌套forRoot

forRoot()模块方法的参数如何传递给提供程序?

如何在NgRx中订阅功能模块中的更改?

在typeorm.forroot中使用process.env变量

将动态对象传递到角度模块的forRoot()

NestJS:在自定义模块中使用forRoot / forChild-竞赛条件?

使用带有angular5的forRoot()和InjectionToken添加库配置

如何在loadChildren()=> XPTOModule中调用forRoot方法?

为什么RouterModule使用forRoot()和forChild()?

使用forRoot将变量传递给模块

在NestJS中创建动态模块时,使用`forRoot`或`register`有什么区别吗?

使用provideIn和forRoot时,存在角度依赖性,圆形依赖性

使用forRoot的Angular库

如何在功能模块中使用导入的forRoot()服务?

如何在forRoot中使用配置数据

使用或不使用 forRoot 导入模块

Angular Elements:可以从功能模块项目结构中使用多个自定义 Web 组件吗?

在功能模块的组件中使用应用模块的组件

Angular - 如何使用`forRoot`将配置传递给另一个模块加载的模块?

如何在动态功能模块中将 SavedStateHandle 注入 ViewModel?