问题
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