Environment Variables In Angular
In this article, we will learn how to set up Angular Environment Variables for various contexts. We will first discover where Angular stores its environment variables. The next step is to make a fresh environment variable. Then, dependent on the environment, we assign various values to this environment variable. We read these values into the Angular Application at the end. Adding more test environments is another skill we develop.
What is Angular Environment
Before going into production, the majority of apps go through many stages. Development, testing, staging, and production are some examples of these phases. These stages are referred to as Environment. Different installations and configurations are needed for each of these environments. For instance, we require our app to be optimised, minified and uglified while you construct the app for production. We don't need any of them for development, but we do want the app to record various debugging details.
What is Angular Environment Variable
The variables that change in value depending on the environment we are in are known as environment variables. This will enable us to adapt the App's behaviour to the surrounding conditions.
For instance, you might want to use various API Endpoints for development, testing, and production. Or perhaps you don't want to transmit such console log messages in a working setting.
Where is Angular Environment Variable
Environment variable configuration and management are supported natively by Angular. The src/environments subdirectory is where it stores the environment configuration.
Two files, one named environment.ts and the other environment.prod.ts are located in the folder.
Angular offers support for the development and production environments right out of the box. The environment is used by default and is the development.ts The environment.prod.ts file is used by the production environment.
You can find the following code by opening the aforementioned files.
//environment.ts
export const environment = {
production: false
};
//environment.prod.ts
export const environment = {
production: true
};
How to Create Environment Variable
//environment.ts
export const environment = {
production: false,
apiEndPoint:"https://api.development.example.com"
};
//environment.prod.ts
export const environment = {
production: true,
apiEndPoint:"https://api.production.example.com"
};
How To Read the Environment Variable
import { environment } from '../environments/environment';
Read the value as it appears below.
apiEndPoint:string="";
constructor() {
this.apiEndPoint = environment.apiEndPoint;
}
Testing the Environment Variable
ng serve
ng serve
ng serve --configuration="production"
ng Build
How does Angular know to switch files?
"production": {
"fileReplacements": [
{
"replace": "projects/variable/src/environments/environment.ts",
"with": "projects/variable/src/environments/environment.prod.ts"
}
],
Create our own configuration
"configurations": {
"production": {
//Removed to make code smaller
},
"test" : {
"fileReplacements": [
{
"replace": "projects/variable/src/environments/environment.ts",
"with": "projects/variable/src/environments/environment.test.ts"
}
]
}
"architect": {
"build": {
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "variable:build"
},
"configurations": {
"production": {
"browserTarget": "variable:build:production"
},
"test": {
"browserTarget": "variable:build:test"
}
}