如何在功能模块层次结构中使用.forRoot() [英] How to use .forRoot() within feature modules hierarchy

查看:61
本文介绍了如何在功能模块层次结构中使用.forRoot()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以向我说明如何使用.forRoot()调用构造多个嵌套的功能模块层次结构吗?

Can anyone please clarify to me how should I structure multiple nested feature modules hierarchy with .forRoot() calls?

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

For example what if I have modules like this:

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

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

All feature modules has a .forRoot() static functions.

如何通过某种方式转移" .forRoot()函数来定义 FeatureModuleA ?

How should I define FeatureModuleA with somehow "transfer" the .forRoot() functions?

@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()函数中进行设置:

I can create another class for root usage then set it within the forRoot() function of FeatureModuleA:

@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 { }

但是如何在此特殊条件下转移" .forRoot()调用 ModuleClass?

But how can I "transfer" .forRoot() calls within this special ModuleClass?

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

As I see I need to import all sub-modules directly into my root MainModule and call .forRoot() for each there:

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

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

Am I right? Before you answer please take a look at this file: https://github.com/angular/material2/blob/master/src/lib/module.ts

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

As I know this repo maintained by the official angular team. So they solve the above with just importing all .forRoot() calls within a special MaterialRootModule module. I don't really understand how it would be applied for my own root module? What does the root and .forRoot really means here? Is that relative to the package and not to the actual web project?

推荐答案

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

Generally forRoot is used to add application/singleton services.

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

原因是,如果将AuthService添加到@NgModule中的providers,则将SharedModule导入其他模块中可能会创建多个.

The reasoning is that if you add the AuthService to the providers in the @NgModule, it's possible for more than one to be created if you import the SharedModule into other modules.

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

I'm not 100% clear on whether the service would be created when the SharedModule is imported into an eagerly loaded module, but the explanation that the docs mentioned was in regards to lazily loaded modules. When you lazily load a module, all the providers will get created.

为此,我们添加(按惯例)forRoot方法以表示该方法仅应在根(应用程序)模块上调用,而对于其他模块,应仅正常导入

For this reason, we add a (by convention) forRoot method to signify that the method should only be called for the root (app) module, while for other module it should just be imported normally

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

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

这篇关于如何在功能模块层次结构中使用.forRoot()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆