意外的令牌导出

Unexpected token export

作为一个新的 Angular 菜鸟,我正在尝试将我以前工作的 Angular 2.0.0-beta17 网络应用程序升级到 Angular 2.3.0。现在我在 loading/booting 应用程序时遇到问题。

这是我的设置:

为了解决问题,我暂时复制了 web 目录中的所有 node_modules。因此不应丢失任何文件。

(web directory)
├── index.html
├── js
│   ├── boot.js
│   ├── boot.js.map
│   ├── feedback-form.component.js
│   ├── feedback-form.component.js.map
│   ├── feedback-form.service.js
│   ├── feedback-form.service.js.map
│   ├── lib
│   │   └── *all node modules*
│   ├── ui-molecule.component.js
│   └── ui-molecule.component.js.map
└── views
    ├── feedback-form.html
    └── ui-molecule.html

package.json

每次更改 package.json 时,我都会丢弃并重建模块文件夹。所以它是最新的。

{
  "name": "FeedbackFormWebApp",
  "version": "0.0.1",
  "author": "Martin Labuschin",
  "description": "...",
  "repository": "...",
  "license": "MIT",
  "devDependencies": {
    "@angular/core": "2.3.0",
    "systemjs": "0.19.41",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.35.2",
    "reflect-metadata": "0.1.8",
    "rxjs": "5.0.0-rc.4",
    "zone.js": "^0.7.2",
    "gulp": "^3.9.0",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-typescript": "3.1.3",
    "gulp-webserver": "^0.9.1",
    "del": "2.2.2",
    "@angular/router": "3.3.0",
    "@angular/common": "2.3.0",
    "@angular/platform-browser": "2.3.0",
    "@angular/platform-browser-dynamic": "2.3.0",
    "@angular/http": "2.3.0",
    "@angular/compiler": "2.3.0",
    "typescript": "2.1.4",
    "minimatch": "3.0.2",
    "graceful-fs": "^4.0.0"
  }
}

index.html(仅脚本部分):

我根据我的 file/directory 结构设置路径。

<script type="text/javascript" src='/js/lib/es6-shim/es6-shim.min.js'></script>
<script type="text/javascript" src="/js/lib/rxjs/bundles/Rx.min.js"></script>
<script type="text/javascript" src="/js/lib/zone.js/dist/zone.min.js"></script>
<script type="text/javascript" src="/js/lib/reflect-metadata/Reflect.js"></script>
<script type="text/javascript" src='/js/lib/systemjs/dist/system.src.js'></script>

<script type="text/javascript">
  System.config({
    packages: {
      js: {
        format: 'register',
        defaultExtension: 'js'
      }
    },
    paths: {
      '@angular/*': 'js/lib/@angular/*/index.js',
      'rxjs/*': 'js/lib/rxjs/*.js'
    }
  });
  System.import('js/boot').catch(function(err){ console.error(err); });
</script>

boot.ts(由我的 gulpfile 成功翻译成 js)尝试在第 1 行加载 angular-core。

import '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { Http, Response, Headers } from '@angular/http';

import { FeedbackFormComponent } from './feedback-form.component';

platformBrowserDynamic().bootstrapModule(FeedbackFormComponent);

System.import (index.html) 之前的所有内容似乎 都已准备妥当。 加载了 lib 文件,没有 404 左右。但这是我不断收到的错误:

> (index):28 Error: (SystemJS) Unexpected token export  SyntaxError:
> Unexpected token export
>       at eval (<anonymous>)
>       at e.invoke (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:15094)
>       at n.run (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:12458)
>       at http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:10100
>       at e.invokeTask (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:15723)
>       at n.runTask (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:13076)
>       at a (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:9146)
>       at XMLHttpRequest.invoke (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:16876)
>   Evaluating http://localhost:8000/js/lib/@angular/core/index.js  Error
> loading http://localhost:8000/js/lib/@angular/core/index.js as
> "@angular/core" from http://localhost:8000/js/boot.js
>       at eval (<anonymous>)
>       at e.invoke (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:15094)
>       at n.run (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:12458)
>       at http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:10100
>       at e.invokeTask (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:15723)
>       at n.runTask (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:13076)
>       at a (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:9146)
>       at XMLHttpRequest.invoke (http://localhost:8000/js/lib/zone.js/dist/zone.min.js:1:16876)
>   Evaluating http://localhost:8000/js/lib/@angular/core/index.js  Error
> loading http://localhost:8000/js/lib/@angular/core/index.js as
> "@angular/core" from http://localhost:8000/js/boot.js

我在 Google 和 Whosebug 上搜索了几个小时,没有解决类似问题的方法适用于这种情况。如何解决?提前致谢!

PS。如有必要,我可以提供存储库。请只问!

正如我们从错误堆栈跟踪中看到的那样,它无法解析 @angular/core/index.js。当您编译自己的源文件时,angular 被部署为具有 ES5 语法的 ES2015 模块。你必须使用纯ES5版本,我建议部署UMD文件。

像这样更新您的 systemjs.config.js:

// map tells the System loader where to look for things
map: {
  // our app is within the app folder
  app: 'app',

  // angular bundles
  '@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 quickstart),这是一个好的开始。或者使用 angular-cli,从长远来看这更好。

来源:https://cdn.rawgit.com/angular/angular.io/b3c65a9/public/docs/_examples/_boilerplate/systemjs.config.web.js