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

查看:53
本文介绍了我可以在构建时将参数传递给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-cli.json
  • 中的apps[0].environments对象
  • 通过build/serve命令上的--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个用于dev的配置文件和6个用于prod的配置文件.

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天全站免登陆