-
Angular
(Using "ngx-environment-variable"): ngxEnv.ENVIRONMENT_VARIABLE -
Node
: process.env.ENVIRONMENT_VARIABLE
npm install ngx-environment-variable OR yarn add ngx-environment-variable
- Put declaration in
src/typings.d.ts
declare var ngxEnv;
- Delete
environment
import statement and put following piece of code above theAppModule
import statement insrc/main.ts
:
import setNgxEnv from 'ngx-environment-variable';
import { environment } from './environments/environment';
setNgxEnv(environment, { origin: 'http://localhost:4200' });
- Use
ngxEnv
to environemt variables defined inenvironment.ts
inside component, directive, pipe and module class.
export class AppComponent {
title = ngxEnv.FACEBOOK_APP_ID;
ngOnInit() {
console.log(ngxEnv.FACEBOOK_APP_ID);
console.log(ngxEnv.production);
}
}
Option | Detail |
---|---|
origin | value provided here is matched with current window origin. If it does not match ngxEnv.<ENV_VAR> will return undefined for all environment variables. |
envVarName | value provided here will rename ngxEnv . Environment variable can be used as <envVarName>.<ENV_VAR> .Warning : do not use any reserved keyword or any window object property. Do NOT forgot to change your declaration statement in src/typings.d.ts . |
Saurav Singh
MIT