Angular2 Outputs in ES5?

女生的网名这么多〃 提交于 2019-12-24 02:59:14

问题


Here is an example code to demonstrate outputs in Angular 2 using ES5. But I get an TypeError: Cannot read property 'subscribe' of undefined error.

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");
        }
    }
})

Seems like there is some error in the template of SampleComponent11. But I am not able to catch it


回答1:


You need to change your code like this:

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)
    }
})

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



来源:https://stackoverflow.com/questions/36895505/angular2-outputs-in-es5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!