本文介绍了ES5中的Angular2输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个示例代码,用于演示使用ES5在Angular 2中的输出.但是出现TypeError: Cannot read property 'subscribe' of undefined错误.

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

似乎SampleComponent11的模板中有一些错误.但是我无法抓住它

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

推荐答案

您需要像这样更改代码:

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

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

这篇关于ES5中的Angular2输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 19:17