我可以在构建时将参数传递给angular-cli吗 [英] Can I pass arguments to angular-cli at build-time
问题描述
在构建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
中的 - 通过build/serve命令上的
--env=NAME
标志使用它们.
apps[0].environments
对象
- create a
src/environments/environment.NAME.ts
- add
{ "NAME": 'src/environments/environment.NAME.ts' }
to theapps[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屋!