Angular 2 AOT 不起作用。为什么 "platformBrowser().bootstrapModuleFactory"会拉取带有附加前缀 "/src/linker/ng_module_factory"的链接?

标签 angular angular2-aot

当我使用 JIT 运行我的 Angular 2 应用程序时,一切正常。 但是在我切换到 AOT 之后,我的应用程序开始使用附加前缀(“/src/linker/ng_module_factory”)提取链接,例如:

JIT 拉取:http://localhost:8080/node_modules/@angular/core/bundles/core.umd.js

AOT 拉取:http://localhost:8080/node_modules/@angular/core/bundles/core.umd.js/src/linker/ng_module_factory postfix /src/linker/ng_module_factory 来自哪里?

关于应用程序。我正在使用 Java 8 + Spring 4.3.2.RELEASE + Angular 2 + Tomcat 9.0.0.M6。

src
--main
----webapp
------resources
--------app
----------app.component.css
----------app.component.ts
----------app.module.ts
----------main.ts
--------index.html
--------package.json
--------systemjs.config.js
--------tsconfig.json
--------typings.json
------WEB-INF
--------dispatcher-servlet.xml
--------web.xml

包.json

{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "postinstall": "typings install"
  },
  "license": "MIT",
  "dependencies": {
    "@angular/common":  "2.0.0",
    "@angular/compiler":  "2.0.0",
    "@angular/core":  "2.0.0",
    "@angular/platform-browser":  "2.0.0",
    "@angular/platform-browser-dynamic":  "2.0.0",

    "systemjs": "0.19.27",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.12",
    "zone.js": "^0.6.23",

    "bootstrap": "^3.3.6"
  },
  "devDependencies": {
    "typescript": "^2.0.2",
    "typings": "^1.0.4"
  },
  "repository": {}
}

index.html

<!DOCTYPE html>
<html>
<head>
  <base href="/">
  <title>MyApp</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <script src="node_modules/core-js/client/shim.min.js"></script>
  <script src="node_modules/zone.js/dist/zone.js"></script>
  <script src="node_modules/reflect-metadata/Reflect.js"></script>
  <script src="node_modules/systemjs/dist/system.src.js"></script>

  <script src="systemjs.config.js"></script>
  <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
</head>

<body>
<my-app>Loading...</my-app>
</body>
</html>

系统js.config.js

(function (global) {
  System.config({
    paths: {
      'npm:': 'node_modules/'
    },
    map: {
      app: 'app',

      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',

      'rxjs': 'npm:rxjs',
    },
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

打字.json

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160725163759"
  }
}

应用程序组件.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <h1>{{title}}</h1>
  `,
  styleUrls: ['app.component.css']
})
export class AppComponent{
    title = 'Title';
}

应用程序模块.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';

@NgModule({
  imports: [
    BrowserModule
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

JIT 配置 主.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);

下一步是使用命令 tsc 进行编译,构建 war 并进行部署。

AOT 配置 主.ts

import { platformBrowser } from '@angular/platform-browser';
import { AppModuleNgFactory } from './app.module.ngfactory';
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

下一步是安装编译器npm install @angular/compiler-cli,使用命令'./node_modules/.bin/ngc'编译,构建war并部署。 ngc 命令后,我得到文件 *.ngfactory.ts、*.ngfactory.js、*.metadata.json、*.shim.ts、*.shim.js

最佳答案

使用 angular2-webpack-starter 不用担心

https://github.com/AngularClass/angular2-webpack-starter

关于Angular 2 AOT 不起作用。为什么 "platformBrowser().bootstrapModuleFactory"会拉取带有附加前缀 "/src/linker/ng_module_factory"的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40070930/

相关文章:

javascript - Angular 2 奥特 : Zoneware (SystemJS) XHR error

angular - AOT编译-forRoot等func调用问题

javascript - 等待 Angular 5 中的 *ngIf 之后元素渲染?

angular - angular2 绑定(bind)中的数学函数

angular - 如何使用可观察对象在 Angular2 中进行嵌套服务调用?

typescript - Angular 2 如何将自定义提供程序注入(inject)服务?

angular - 在不破坏 AoT 的情况下动态加载组件 - Angular2

使用 isDevMode 的 Angular AOT 给出错误

javascript - 类具有或正在使用外部模块中的名称 'SafeUrl' 但无法命名

angular - 在 Angular 的整个应用程序中发出和广播事件