ES5 中的 Angular2 输出?

Angular2 Outputs in ES5?

这是一个示例代码,用于演示使用 ES5 在 Angular2 中的输出。但是我收到 TypeError: Cannot read property 'subscribe' of undefined 错误。

var SampleComponent10 = ng.core.Component({
    selector: "sampleten",
    outputs: ["click"],
    template: ""
}).Class({
    constructor: function(){
        setInterval(function(){
            this.click.next({});
        }.bind(this), 10000)
    }
})

var SampleComponent11 = ng.core.Component({
    selector: "sampleeleven",
    directives: [SampleComponent10],
    template: "<sampleten (click)='clicked($event)'></sampleten>"
}).Class({
    constructor: function(){
        this.clicked = function(e){
            console.log(e);
            console.log("Clicked");
        }
    }
})

SampleComponent11 的模板似乎有问题。但是我抓不到它

您需要像这样更改您的代码:

var SampleComponent10 = ng.core.Component({
    selector: "sampleten",
    outputs: ["click"],
    template: ""
}).Class({
    constructor: function(){
        this.click = new ng.core.EventEmitter(); <== add this line
        setInterval(function(){
            this.click.next({});
        }.bind(this), 10000)
    }
})

另见 https://angular.io/docs/ts/latest/cookbook/ts-to-js.html#!#property-metadata