Javascript 在 Angular2 rc3 中使用 ES6 时需要未捕获的反射元数据垫片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38054090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught reflect-metadata shim is required when using ES6 with Angular2 rc3
提问by rocketer
I just updated Angular from rc-1
to latest rc-3
.
The app is using JavaScript ES6and SystemJS. When I run the app with browsersync, it works. But if I bundle the app (with systemjs-builder) and then run it, I have this error in the browser console
我刚刚将 Angular 更新rc-1
为 latest rc-3
。该应用程序使用 JavaScript ES6和 SystemJS。当我使用 browsersync 运行应用程序时,它可以工作。但是,如果我捆绑应用程序(使用 systemjs-builder)然后运行它,我会在浏览器控制台中出现此错误
Uncaught reflect-metadata shim is required when using class decorators.
使用类装饰器时需要未捕获的反射元数据垫片。
The problem comes from a component using @angular/http
with a basic http call, if I remove import {Http, HTTP_PROVIDERS} from '@angular/http' ;
it works.
问题来自使用@angular/http
基本 http 调用的组件,如果我删除import {Http, HTTP_PROVIDERS} from '@angular/http' ;
它的话。
Plus, it does not happen with TypeScript but it does with JS ES5 and ES6. Also it doesn't happen with Webpack.
另外,TypeScript 不会发生这种情况,但 JS ES5 和 ES6 会发生这种情况。Webpack 也不会发生这种情况。
I looked into the bundled code and it appears that SystemJS goes through Angular
code before Reflect
code... only with es6
我查看了捆绑的代码,似乎 SystemJS 在Angular
代码之前先通过Reflect
代码......仅使用 es6
index.js
索引.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
应用程序.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]];
}
}
回答by Mark Langer
reflect-metadata
, es6-shim
and zone.js
are supposed to be global libraries. Consequently, you should not import them into one of your modules like you do in index.js.
reflect-metadata
,es6-shim
并且zone.js
应该是全局库。因此,您不应像在 index.js 中那样将它们导入您的模块之一。
Try removing the import statements in your index.js and reference them in your index.html like it is explained in the Angular 2 Quickstart:
尝试删除 index.js 中的 import 语句并在 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>