Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Environment variables on compilation step in Angular CLI

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

E.g.:

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

How can I access API_URL in Angular components?

Thanks in advance!

like image 929
wexpixies Avatar asked Aug 16 '26 17:08

wexpixies


1 Answers

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

src\environments.ts

For production, use the"

src\environments.prod.ts

When you build the application with:

ng build -prod 

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

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

like image 79
Ben Richards Avatar answered Aug 18 '26 06:08

Ben Richards