Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular CLI中的编译步骤上访问环境变量_Angular_Environment Variables_Angular Cli - Fatal编程技术网

在Angular CLI中的编译步骤上访问环境变量

在Angular CLI中的编译步骤上访问环境变量,angular,environment-variables,angular-cli,Angular,Environment Variables,Angular Cli,使用ng serve时,是否有任何方法访问angular 2组件中的环境变量 例如: API_URL=http://api.test.com ng serve 如何访问角度组件中的API_URL 提前谢谢 CLI为此目的提供了“环境”文件。为了便于开发,请将配置放在文件中: src\environments.ts 对于生产,请使用“ 使用以下工具构建应用程序时: ng build -prod (或者只是ng build,因为-prod是默认值) Angular将使用environment

使用
ng serve
时,是否有任何方法访问angular 2组件中的环境变量

例如:

API_URL=http://api.test.com ng serve
如何访问角度组件中的API_URL

提前谢谢

CLI为此目的提供了“环境”文件。为了便于开发,请将配置放在文件中:

src\environments.ts
对于生产,请使用“

使用以下工具构建应用程序时:

ng build -prod 
(或者只是ng build,因为-prod是默认值)


Angular将使用environment.prod.ts文件代替environment.ts文件。

感谢回复Boyan!当我使用environments.ts文件时,我应该事先知道环境变量的值,以描述每个环境中的每个变量,但主要目的是API\U URL可以动态更改,以及我如何直接访问它n组件,而不为您要求的环境添加新值,这是可以做到的,但它很快就会变得更复杂。请查看我关于在运行时提供配置的另一个答案。
ng build -prod