Developer Snippet Diary

Environment file in angular| variables, functions etc

Using the Angular CLI, start by running the generate environments command shown here to create the src/environments/ directory and configure the project to use these files.

ng generate environments

The project's src/environments/ directory contains the base configuration file, environment.ts, which provides configuration for production, the default environment. You can override default values for additional environments, such as development and staging, in target-specific configuration files.

project\src\environments.ts

export const environment = {
  production: false ,
  domainURL: "http://localhost:4200",
  site_details:{
    title:"My site",
    description:"My site is awesome"
  }
};

The main CLI configuration file, angular.json, contains a fileReplacements section in the configuration for each build target.

"configurations": {
  "development": {
    "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.development.ts"
        }
      ],
      …

This means that when you build your development configuration with ng build --configuration development, the src/environments/environment.ts file is replaced with the target-specific version of the file, src/environments/environment.development.ts.

You can add additional configurations as required. To add a staging environment, create a copy of src/environments/environment.ts called src/environments/environment.staging.ts, then add a staging configuration to angular.json:

Posted by: R GONDAL
Email: rizikmw@gmail.com