Issue
I run this Angular app in Google cloud platform to test if the environment is dev
or prod
. I use the isDevMode()
like this:
export class BookService {
private BookItemsUrl: string; // URL to web api
constructor(private http: HttpClient) {
// Return different backend path depending on runtime environment.
if (isDevMode()) {
// Localhost
this.BookItemsUrl = 'api/BooksXml';
} else {
this.BookItemsUrl = 'https://my-back-bone.appspot.com/api/BooksXml';
}
}
..............
This works as expected on localhost, but not when deployed to GCP.
I searched but can’t see why this won’t work.
Any idea?
Solution
From angular docs about isDevMode
:
By default, this is true, unless a user calls
enableProdMode
before
calling this.
So in order to make isDevMode
work correctly, you must invoke enableProdMode()
in production enviromnet before using isDevMode()
.
One way to enable runtime production mode is to use --prod
flag when you make your production build, as Sergey mentioned.
Make sure you include to following lines, if not already included in your main.ts
file. This answer can be helpful as well.
if (environment.production) {
enableProdMode();
}