注射器不能在角度2.0中工作

Alv*_*das 14 typescript1.5 angular

我最近开始玩Angular2.我一直试图让注射剂工作半天左右,但我仍然无法弄清楚我做错了什么.

为了尽可能简单,我在官方网页上复制了5 Min Quickstart中的代码.演示本身工作正常,但当我尝试使用注射剂时,我得到一个错误说

ORIGINAL ERROR:无法解析MyAppComponent的所有参数.确保它们都具有有效的类型或注释.

我的打字稿文件

/// <reference path="typings/angular2/angular2.d.ts" />
import {Component, View, bootstrap,} from 'angular2/angular2';

class Names {}

// Annotation section
@Component({
    selector: 'my-app',
    injectables: [Names]
})
@View({
    template: '<h1>Hello {{ name }}</h1>'
})
// Component controller
class MyAppComponent {
    name: string;
    constructor(names: Names) {
        this.name = 'Alice';
    }
}

bootstrap(MyAppComponent);
Run Code Online (Sandbox Code Playgroud)

PS和5 Min Quickstart一样,我正在使用Traceur,SystemJSAngular2 alpha (23)

有谁知道我错过了什么?

ale*_*ods 12

您的编译器不会向MyAppComponent(从查看您的pluker)添加参数属性.我认为这是问题所在.如果你添加

MyAppComponent.parameters = [[Names]]
Run Code Online (Sandbox Code Playgroud)

一切都会好起来的.

  1. 是你的修补程序.
  2. 是TS中的相同示例(带ES6)

UPD感谢@GrayFox指出了正确的方法(参见下面的评论):

对于将来的引用 - --emitDecoratorMetadata在使用tsc时使用flag或emitDecoratorMetadata: true如果您正在使用gulp-typescript则添加到配置

在此处查看TypeScript编译器选项(您可以在emitDecoratorMetada那里找到).

  • 对于未来的参考 - 使用`tsc`时使用'--emitDecoratorMetadata`标志或添加`emitDecoratorMetadata:如果您在使用`一饮而尽,typescript` TRUE`的配置. (4认同)