将 ES6 与 Angular2 rc3 一起使用时需要未捕获的反射元数据垫片

Uncaught reflect-metadata shim is required when using ES6 with Angular2 rc3

我刚刚将 Angular 从 rc-1 更新到最新的 rc-3。 该应用程序使用 JavaScript ES6 和 SystemJS。当我 运行 带有 browsersync 的应用程序时,它可以工作。但是如果我捆绑应用程序(与 systemjs-builder)然后 运行 它,我在浏览器控制台中出现此错误

Uncaught reflect-metadata shim is required when using class decorators.

问题来自使用 @angular/http 和基本 http 调用的组件,如果我删除 import {Http, HTTP_PROVIDERS} from '@angular/http' ; 它会起作用。

此外,TypeScript 不会发生这种情况,但 JS ES5 和 ES6 会发生。 Webpack 也不会发生这种情况。

我查看了捆绑代码,似乎 SystemJS 在 Reflect 代码之前通过了 Angular 代码...仅使用 es6

index.js

import 'reflect-metadata';
import 'es6-shim';
import 'zone.js';
import {bootstrap} from '@angular/platform-browser-dynamic';
import {App} from './app.js';
bootstrap(App);

app.js

import {Component} from '@angular/core';
import {Http, HTTP_PROVIDERS} from '@angular/http';
@Component({
  selector: 'App',
  template: '',
  providers: [HTTP_PROVIDERS]
})
export class App {
  constructor(http) {}

  static get parameters() {
    return [[Http]];  
  }
}

reflect-metadataes6-shimzone.js 应该是全局库。因此,您不应像在 index.js.

中那样将它们导入您的模块之一

尝试删除 index.js 中的导入语句并在 index.html 中引用它们,就像在 Angular 2 Quickstart:

中解释的那样
<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>