Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using angular4(4.4.6) and CLI 1.4.3. I tried to make environment variables like in this article: https://alligator.io/angular/environment-variables/

I ended up with 3 files: environment.ts

export const environment = {
  production: false,
  restUrl: 'http://localhost:3000/',
  socketUrl: 'http://localhost:2000'
};

environment.prod.ts

export const environment = {
  production: true,
  restUrl: 'http://139.130.4.5:3000/',
  socketUrl: 'http://139.130.4.5:2000'
};

and environment.staging.ts

export const environment = {
  production: true,
  restUrl: 'http://139.130.4.5:3000/',
  socketUrl: 'http://139.130.4.5:2000'
};

I use them as follows:

import { environment } from '../../environments/environment';
 constructor() {
    this.serverUrl = environment.restUrl;
    console.log('the env is:' this.serverUrl');
  }

and in .angular-cli.json I have the following:

"environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts",
        "staging": "environments/environment.staging.ts"
      }

and from some reason, when I run ng build --env=prod it compiles and everything, but the final product uses the localHost(dev) enviorment variables.

whats even more strange is that when I use ng server --env=prod it works perfectly, with production variables.

why is this happening? how can I build with prod or staging environment variables?

question from:https://stackoverflow.com/questions/48153669/ng-build-env-prod-is-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
816 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use command like this for Anuglar 6 to build

ng build --prod --configuration=dev

Or using alias:

ng build --prod --c=dev

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...