Stephan K.
Stephan K.

Reputation: 15712

HowTo not hardcode Angular2 environment variables like API Urls

With Angular2 you can use src/environments/environment.ts and *.prod.ts to configure either production or development environments. Now we have a huge staging requirements and we will need e.g. 5-10 API Urls. How to set and read environment variables other than --prod or --dev with angular-cli, e.g. URL="http://server12a"?

Upvotes: 2

Views: 459

Answers (1)

S. Robijns
S. Robijns

Reputation: 1599

You can have all different kind of environments. Define each of those in your angular-cli.json file:

  "environments": {
    "local": "environments/environment.ts",
    "test": "environments/environment.test.ts",
    "staging ": "environments/environment.staging.ts",
    "prod": "environments/environment.prod.ts"
  }

Within these files you can have something along the lines of:

export const environment = {
  URL="http://server12a"
};

When you build you have to specify the right environment like:

ng build --env=staging

Upvotes: 3

Related Questions