我可以在构建时将参数传递给 angular-cli [英] Can I pass arguments to angular-cli at build-time

查看:28
本文介绍了我可以在构建时将参数传递给 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.

这篇关于我可以在构建时将参数传递给 angular-cli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆