【问题标题】:Angular 4 AOT build issue, bootstrap errorAngular 4 AOT 构建问题,引导错误
【发布时间】:2017-10-17 22:18:15
【问题描述】:

我使用 Angular CLI 创建了一个 Angular 4 hello world 应用程序,我想尝试使用 AOT/WEBPACK 构建的生产版本,但我无法使其工作。我按照 angular.io 网站 (https://angular.io/docs/ts/latest/cookbook/aot-compiler.html) 中的步骤进行操作。当我构建时,出现引导错误。

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

package.json

{
  "name": "angular2-aot",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.0.0",
    "@angular/compiler-cli": "^4.0.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "~6.0.60",
    "codelyzer": "~2.0.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.0",
    "rollup": "^0.41.6",
    "rollup-plugin-commonjs": "^8.0.2",
    "rollup-plugin-node-resolve": "^3.0.0",
    "rollup-plugin-uglify": "^2.0.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "~2.2.0"
  }
}

index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Angular2-AOT</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root>Loading...</app-root>
</body>
</html>

ma​​in.ts

import { enableProdMode } from '@angular/core';
import { platformBrowser }    from '@angular/platform-browser';
import { AppModuleNgFactory } from '../aot/src/app/app.module.ngfactory';

import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

当我构建时,我遇到了错误

ng build --prod

Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.
Error: Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.
    at Object.resolveEntryModuleFromMain (E:\Angular2\angular2-aot\node_modules\@ngtools\webpack\src\entry_resolver.js:128:11)
    at AotPlugin._setupOptions (E:\Angular2\angular2-aot\node_modules\@ngtools\webpack\src\plugin.js:142:50)
    at new AotPlugin (E:\Angular2\angular2-aot\node_modules\@ngtools\webpack\src\plugin.js:26:14)
    at _createAotPlugin (E:\Angular2\angular2-aot\node_modules\@angular\cli\models\webpack-configs\typescript.js:55:12)
    at Object.exports.getAotConfig (E:\Angular2\angular2-aot\node_modules\@angular\cli\models\webpack-configs\typescript.js:89:19)
    at NgCliWebpackConfig.buildConfig (E:\Angular2\angular2-aot\node_modules\@angular\cli\models\webpack-config.js:26:37)
    at Class.run (E:\Angular2\angular2-aot\node_modules\@angular\cli\tasks\build.js:26:92)
    at Class.run (E:\Angular2\angular2-aot\node_modules\@angular\cli\commands\build.js:143:26)
    at Class.<anonymous> (E:\Angular2\angular2-aot\node_modules\@angular\cli\ember-cli\lib\models\command.js:134:17)
    at process._tickCallback (internal/process/next_tick.js:109:7)

【问题讨论】:

  • @yurzui,不,我只是按照 Angular.io 中的步骤操作。
  • 你使用 angular-cli。阅读有关 angular-cli 的生产构建
  • @yurzui,我正在使用 angular-cli
  • 这看起来像 Angular 4,而不是 Angular 2 - 可能想提一下,因为这可能会影响问题的回答方式。

标签: angular


【解决方案1】:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModuleNgFactory } from '../aot/src/app/app.module.ngfactory';
import { BSystemService } from './bGlobal/BSystemService';

let bSystemService: BSystemService = BSystemService.getInstance();

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

if (bSystemService.deviceType == 'MOBILE') {
document.addEventListener('deviceready', function() {
bootstrapNow();
});
} else {
bootstrapNow();
}

function bootstrapNow() {
platformBrowserDynamic().bootstrapModule(AppModuleNgFactory);
}`

【讨论】:

    【解决方案2】:

    请做一件事运行 npm -g install @angular/cli

    然后创建生产版本

    可能是 angular cli 旧版本问题

    如果你仍然发现问题,那么将下面的代码放在你的 main.js 中

    import { WfaNg2Module } from './app';
    

    改变

    platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
    

    到

    document.addEventListener('DOMContentLoaded', function () {
      platformBrowserDynamic().bootstrapModule(WfaNg2Module, []);
    }, false);
    

    如果这没有帮助,请告诉我

    【讨论】:

      【解决方案3】:

      您不必再使用模块:module.id。它适用于以前的 Angular 2 版本。

      见->https://angular.io/docs/ts/latest/cookbook/aot-compiler.html

      组件相关的模板网址

      AOT 编译器要求外部模板和 CSS 文件的 @Component URL 是组件相关的。这意味着@Component.templateUrl 的值是相对于组件类文件的URL 值。例如,“app.component.html”URL 意味着模板文件是其配套 app.component.ts 文件的兄弟。

      虽然 JIT 应用 URL 更加灵活,但请坚持使用相对于组件的 URL 以与 AOT 编译兼容。

      【讨论】:

      • 在链接中转到这里 -> AOT 快速入门源代码 这是相关的源代码。您需要使用 rollup-config.js.. 创建一个 tsconfig-aot.json.. make rollup 文件并将其添加到 index.html ->
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2018-02-23
      • 1970-01-01
      相关资源
      最近更新 更多