Detect Debug/Production Mode
By default @angular/cli
generate file ~/src/environments/environment{.prod,}.ts
:
export const environment = {
production: true
};
We can import environment.production
boolean value to tell current running mode.
It is okay for an application, but not for a library, which can not import variable defined in end application. Here is the solution:
import { isDevMode } from '@angular/core';
@angular/cli
generated file src/main.ts
init dev mode from environment.production
variable:
if (environment.production) {
enableProdMode();
}
NOTE: Must call isDevMode()
when needed, if isDevMode()
called before enableProdMode()
, isDevMode()
locked in dev mode, and raised an error:
export function enableProdMode(): void {
if (_runModeLocked) {
throw new Error('Cannot enable prod mode after platform setup.');
}
_devMode = false;
}
网友评论