访问Angular CLI中编译步骤的环境变量 [英] Access Environment variables on compilation step in Angular CLI

查看:228
本文介绍了访问Angular CLI中编译步骤的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 ng服务时,是否有任何方式访问角度2组件的环境变量?

Is there any way to access environment variable in angular 2 components when using ng serve?

例如:

API_URL=http://api.test.com ng serve

如何在角度组件中访问API_URL?

How can I access API_URL in Angular components?

提前感谢! >

Thanks in advance!

推荐答案

CLI为此提供了环境文件。对于开发,将您的配置放在文件中:

The CLI provides "environment" files just for that purpose. For development, put your configuration in the file:

src\environments.ts

对于生产,请使用

src\environments.prod.ts

当您使用以下版本构建应用程序时:

When you build the application with:

ng build -prod 

(或者只是ng构建,因为-prod是默认的)

(or just ng build since -prod is the default)

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

Angular will use the environment.prod.ts file in place of the environment.ts file.

这篇关于访问Angular CLI中编译步骤的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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