簡體   English   中英

Angular 4 AOT構建問題,引導錯誤

[英]Angular 4 AOT build issue, bootstrap error

我使用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>

main.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)

請做一件事運行npm -g install @ angular / cli

然后創建生產構建

它可能是角度cli舊版本問題

如果你仍然發現問題,那么將下面的代碼放在你的main.js中

import { WfaNg2Module } from './app';

並改變

platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

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

如果沒有幫助,請告訴我

您不必再使用module:module.id了。 這是針對previuos Angular 2的版本。

請參閱 - > https://angular.io/docs/ts/latest/cookbook/aot-compiler.html

組件相對模板URLS

AOT編譯器要求外部模板和CSS文件的@Component URLS是組件相關的。 這意味着@ Component.templateUrl的值是相對於組件類文件的URL值。 例如,“app.component.html”URL表示模板文件是其配套app.component.ts文件的兄弟。

雖然JIT應用程序URL更靈活,但為了與AOT編譯兼容,請堅持使用組件相對URL。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM