我可以在构建时将参数传递给 angular-cli

问题描述:

我想在构建 Angular2(打字稿)应用程序时将自定义参数传递给 angular-cli.这可能吗?如何在我的代码中访问这个参数?

I would like to pass custom arguments to angular-cli when building an Angular2 (typescript) app. Is this possible? How can I access this arguments in my code?

场景是这样的:我有一个带有 2 个布局的 Angular2 应用程序.每个布局有 3 种颜色(红色、蓝色、绿色).我想建立所有可能的组合.每个布局和颜色一个应用程序 => layout1red, layout1green, layout2blue, ...

The scenario is like this: I have one Angular2 app with 2 layouts. Each layout has 3 colors (red, blue, green). I want to build all possible combinations. One app per layout and color => layout1red, layout1green, layout2blue, ...

我想为我定义布局和颜色的每个构建创建 6 个 JSON 配置文件,也许还有一些附加属性.

I want to create 6 JSON config files for each build where I define the layout and color, and maybe some additional properties.

可以使用 @angular/cli 创建多个配置文件.

It is possible to create multiple configuration files with @angular/cli.

文档中所述自定义配置文件可以通过以下方式添加:

As mentioned in docs custom configuration files can be add in the following way:

  • 创建一个 src/environments/environment.NAME.ts
  • { "NAME": 'src/environments/environment.NAME.ts' } 添加到 .angular 中的 apps[0].environments 对象-cli.json
  • 通过构建/服务命令上的 --env=NAME 标志使用它们.
  • create a src/environments/environment.NAME.ts
  • add { "NAME": 'src/environments/environment.NAME.ts' } to the apps[0].environments object in .angular-cli.json
  • use them via the --env=NAME flag on the build/serve commands.

因此,您可能需要 6 个用于开发的配置文件和 6 个用于生产的配置文件.

So, you would probably need 6 config files for dev and 6 config files for prod.

然后访问这些变量只需从 environment.ts 文件中导入环境对象.

And then to access those variables just import environment object from environment.ts file.