Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want some variables that Angular 4 uses to be different depending on whether I am running the app on the production server or on localhost for development. How can I do this? For node.js, I use environment variables but I am not sure if it is possible to use similar environment variables for an Angular web app. What is the best way to approach setting Angular to production without explicitly setting it then deploying?

like image 388
s_kirkiles Avatar asked Jun 28 '17 19:06

s_kirkiles


1 Answers

If you called the enableProdMode() method 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");
    }
  }
}

Thats one way to check the app mode.

But something that is closer related to environment values are the environment files generated by the angular-cli. With those files you can configurate values that will be setted depending on the mode that you start the live server/build your source. You can find more info in the following link

like image 187
Jota.Toledo Avatar answered Sep 16 '22 12:09

Jota.Toledo