任何人都可以向我澄清如何使用.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项目?
我不知道你为什么要创建和使用'forRoot'方法..也许这有助于:https://angular.io/docs/ts/latest/guide/ngmodule.html – mxii
是的。我阅读了官方文档。没有任何关于如何用多个嵌套模块来构建复杂项目。我想创建并使用forRoot,因为提供者单例在整个项目中只应该安装一次。 – ggabor
好的,因为在你的例子中没有提供者。 :) – mxii