Angular2 - 组和有选择地添加模块声明

问题描述:

我们正在使用angular2和角度cli,并且我们的应用程序变得相当大,并且在app.module.ts文件中有大量declarations:Angular2 - 组和有选择地添加模块声明

@NgModule({ 
    declarations: [ 
    GameComponent1, 
    GameComponent2, 
    GameComponent3, 
    StatsComponent1, 
    StatsComponent2, 
    StatsComponent3, 
    AboutPageComponent1, 
    AboutPageComponent2, 
    AboutPageComponent3, 
    AboutPageComponent4, 
    ],... 

首先,有一种方法将相关组件声明如下所示:

@NgModule({ 
    declarations: [ 
    GameComponentsGroup, 
    StatsComponentsGroup, 
    AboutPageComponentsGroup, 
    ],... 

然后,它可以是能够选择性地从被包括在构建和关闭组件切换,针对不同的环境。

if(environment.envName == "developmentWithStats") 
    appmodule add StatsComponentsGroup 

目标是让不同的功能包含在不同的版本中。

我知道我错过了这里的一些关键知识,任何帮助将不胜感激。

首先,有没有办法将相关的组件声明如下所示:

你可以将它们组合成不同的模块

@NgModule({ 
    declarations: [ Game1Component, Game2Component ], 
    exports: [ Game1Component, Game2Component ] 
}) 
class GameModule {} 

@NgModule({ 
    imports: [ GameModule ] 
}) 
class ModuleThatUsesGameModule {} 

然后难道可以选择性地打开和关闭构件中包含的组件,以适应不同的环境。

你可以做类似

const declarations = [ 
    CoreComponent 
]; 

if(environment.envName == "developmentWithStats") { 
    declarations.push(SomeComponent); 
} 

@NgModule({ 
    declarations: declarations 
}) 
+0

哇,这正是我需要的!感谢您的时间来回答这个问题 –