我最近开始玩Angular2。我已经试着注射了半天了,但是我还是不知道我做错了什么。
为了尽可能简单,我在官方网页上复制了5 Min Quickstart中的代码。演示本身工作得很好,但是当我尝试使用可注射的东西时,我会错误地说
原始错误:无法解析myappcomponent的所有参数。制作
确保它们都具有有效的类型或批注。
我的typescript文件

/// <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);

p.s.在5 Min Quickstart中,我使用TraceurSystemJSAngular2 alpha (23)
有人知道我错过了什么吗?

最佳答案

编译器不会将参数属性添加到MyAppComponent(从查看您的AA>)。我想这就是问题所在。如果添加

MyAppComponent.parameters = [[Names]]

那么一切都会好起来的。
pluker是你的抢匪。
Here是TS(ES6)中的一个例子
UPD感谢@灰色狐指出正确的方式(见评论贝娄):
对于将来的参考-在使用tsc时使用--emitDecoratorMetadata标志,或者在使用gulp typescript时将emitDecoratorMetadata: true添加到配置中
请参阅typescript编译器选项Here(您可以在那里找到emitDecoratorMetada)。

09-30 14:53
查看更多