webpack 平台设置后无法启用生产模式,Angular 5生产构建

yiytaume  于 2022-12-13  发布在  Webpack
关注(0)|答案(2)|浏览(151)

错误:

Uncaught Error: Cannot enable prod mode after platform setup. at main.bundle.js:1

** Package 盒:**

在使用**ng build --prod生成构建后,应用程序启动时出错。但是,使用ng serveng build**编译的开发构建都运行正常。

我尝试的是:

  • 按照this答案,将全局和本地@angular/cli升级到最新版本,即1.6.0

软件包.json:

{
  "dependencies": {
    "@angular/animations": "^5.0.0",
    "@angular/common": "^5.0.0",
    "@angular/compiler": "^5.0.0",
    "@angular/core": "^5.0.0",
    "@angular/forms": "^5.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "^5.0.0",
    "@angular/platform-browser-dynamic": "^5.0.0",
    "@angular/router": "^5.0.0",
    "auth0-js": "^8.11.3",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "jquery": "^3.2.1",
    "rxjs": "^5.5.2",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "^1.6.0",
    "@angular/compiler-cli": "^5.0.0",
    "@angular/language-service": "^5.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/jquery": "^3.2.16",
    "@types/node": "~6.0.60",
    "@types/rx": "^4.1.1",
    "codelyzer": "~3.2.0",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "^2.6.2"
  }
}

主文件.ts

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule).catch(err => console.log(err));

重现错误的最小代码:

git/cli-reproapp

xtfmy6hx

xtfmy6hx1#

在您在git/cli-reproapp提供的示例应用中,由于以下行而发生错误:-
如果是,则返回一个值。
在文件cli-reproapp/src/app/app.constants.ts中。因此,作为一种解决方法,您可以创建一个函数来代替常量isInDevMode,如下所示。这样,当代码中实际使用isDevMode()时,它会在以后被调用(最重要的是在调用enableProdMode()之后)。

export function isInDevMode():boolean {
    return isDevMode();
}

很明显,在此之后,您还需要将使用“isInDevMode”常量的行更改为对新创建的“isInDevMode()”方法的调用。

this.title = isInDevMode ? 'App in dev mode works.' : 'App is prod mode works.'

与这一个:-

this.title = isInDevMode() ? 'App in dev mode works.' : 'App is prod mode works.'

您还可以在链接git/cli-reproapp(working)处查看示例应用的工作版本。

nfg76nw0

nfg76nw02#

当我在app.module中使用函数isDevMode()并构建我的应用程序时,我发现了这个错误。我用environment.production替换了它,它解决了我的这个问题。

相关问题