How can I check if Angular is running on production server against localhost

I want some of the variables used by Angular 4 to be different depending on whether I am running the application on a production server or on a local host for development. How can i do this? For node.js, I'm using environment variables, but I'm not sure if it is possible to use similar environment variables for an Angular web app. What's the best way to get close to configuring Angular for release without explicitly configuring its deployment?

+6


source to share


2 answers


If you called a method enableProdMode()

for example in your main.ts file, you can import isDevMode

from @angular/core

to check if the angular app is running on prod mod or not.

for example

import { Component, isDevMode} from '@angular/core';

@Component({...})
export class HomePageComponent {
  constructor() {
    if(isDevMode()){
        console.log("created new HomePageComponent");
    }
  }
}

      



This is one way to check the application mode.

But what is closer to the environment values ​​are the environment files generated by angular-cli. With these files you can customize the values ​​that will be set depending on the mode in which you start the live server / build source. You can find more information at the following link

+7


source


The standard project created by Angular-Cli has a class named "environment". You will see that you can add different values ​​to different versions of the class (one of them is "production") and Cli will use the correct value when it starts

ng build - - prod

More details can be found in the doc: https://angular.io/guide/deployment#enable-production-mode.



Use this code to check the working mode:

import { environment } from '../environments/environment';

console.log(environment.production);

      

+1


source







All Articles