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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With