ES5中的Angular2输出?

时间:2016-04-27 16:20:23

标签: angular angular2-template

以下是使用ES5演示Angular 2中输出的示例代码。但是我收到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的模板中存在一些错误。但我无法抓住它

1 个答案:

答案 0 :(得分:1)

您需要像这样更改代码:

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