ES5 中的 Angular2 输出? [英] Angular2 Outputs in ES5?

查看:26
本文介绍了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 输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆