美文网首页
Tips of Angular

Tips of Angular

作者: forks1990 | 来源:发表于2018-11-22 10:48 被阅读0次

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;
}

相关文章

网友评论

      本文标题:Tips of Angular

      本文链接:https://www.haomeiwen.com/subject/iqnkqqtx.html