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

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

问题描述

在使用 ng serve 时,有没有办法访问 angular 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

如何在 Angular 组件中访问 API_URL?

How can I access API_URL in Angular components?

提前致谢!

推荐答案

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 build,因为 -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天全站免登陆