2016-09-23 43 views
43

任何人都可以向我澄清如何使用.forRoot()调用构建多个嵌套的要素模块层次结构?如何在要素模块层次结构中使用.forRoot()

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

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

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

如何定义FeatureModuleA以某种方式“传递”.forRoot()函数?

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

我可以创建另一个类为根的使用则forRoot内设置()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?

当我看到我需要直接导入所有的子模块到我的根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项目?

+0

我不知道你为什么要创建和使用'forRoot'方法..也许这有助于:https://angular.io/docs/ts/latest/guide/ngmodule.html – mxii

+1

是的。我阅读了官方文档。没有任何关于如何用多个嵌套模块来构建复杂项目。我想创建并使用forRoot,因为提供者单例在整个项目中只应该安装一次。 – ggabor

+0

好的,因为在你的例子中没有提供者。 :) – mxii

回答

79

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

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

的理由是,如果你在@NgModule添加AuthServiceproviders,有可能为一个以上的,如果你导入SharedModule到另一个模块中创建。

我并不十分清楚在将SharedModule导入到急切加载的模块中时是否会创建服务,但所提及的文档与延迟加载模块有关的解释。当你懒洋洋地加载一个模块时,所有的提供者都会被创建。

出于这个原因,我们添加了(按照约定)forRoot方法,以表明该方法只应称为根(应用)模块,而对于其他模块它应该只是正常的进口

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

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

感谢您的努力,但这仍然不关心多级嵌套模块的情况。在您的示例中,您将AuthService导入了SharedModule。你可以重新引导你的例子,导入SharedModule(模块,而不是服务!) - 并尝试在某处使用AuthModule.forRoot()。 (免责声明:对于我来说,AuthService/AuthModule只是一个例子 - 它可以是任何带有forChild和forRoot方法的模块。) – ggabor

+3

forRoot只能用于应用程序模块。应该不需要转发任何东西。当您在没有forRoot的情况下导入Just SharedModule时,永不会调用forRoot。我们应该这样构造我们的模块。 forRoot中的应用程序范围服务以及NgModule中的所有其他服务。嵌套与否,这是您应该考虑的方法 –

+0

如果您需要将功能模块导入其他功能模块,请继续操作,但不会受到影响。请记住关于forRoot中关于应用程序级别提供程序的经验法则,并且您应该很好 –