2016-04-27 62 views
1

以下是使用ES5演示Angular 2中输出的示例代码。但是我得到一个TypeError: Cannot read property 'subscribe' of undefined错误。ES5中的Angular2输出?

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

你需要改变这样的代码:

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

+0

但是为什么'outputs'财产不能正常工作,请参阅。为什么我们需要手动将其分配给EventEmitter的新实例? –

+0

因为您的类组件的属性点击是未定义的。 – yurzui

+0

而这条线会导致错误 – yurzui