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

Angular2 - Group and selectively add module declarations

我们正在使用 angular2 和 angular-cli,我们的应用程序变得相当大并且在 app.module.ts 中有大量 declarations:文件。

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

首先,有没有办法像下面这样对相关的组件声明进行分组:

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

那么是否可以针对不同的环境有选择地打开和关闭包含在构建中的组件。

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

目标是让不同的功能包含在不同的构建中。

我知道我在这里遗漏了一些关键知识,我们将不胜感激。

First of all, is there a way to group related component declarations like the following:

您可以将它们分组到不同的模块中

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

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

Then could it be possible to selectively switch on and off components from being included in the build, for different environments.

你可以这样做

const declarations = [
  CoreComponent
];

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

@NgModule({
  declarations: declarations
})