es6类. this.addeventlistener [英] es6 class. this.addeventlistener

查看:354
本文介绍了es6类. this.addeventlistener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个班级中添加一个事件侦听器,但一直出现此错误:

I am trying in a class to add an eventlistener but keep getting this error:

"TypeError:this.win.addEventListener不是函数"

"TypeError: this.win.addEventListener is not a function"

我该如何解决?

代码:

class Ui {
    constructor(el) {
        this.win = $(window);
        this.onResize = this.onResize.bind(this);
        this.init();
    }

    init() {
        console.log(this.win);
        this.addListeners();
    }

    addListeners() {
        this.win.addEventListener('resize', this.onResize);
    }

    onResize() {
        console.log('test');
    }
}

export default Ui;

推荐答案

jQuery实例没有addEventListener方法.改为使用 on .

jQuery instances do not have addEventListener method. Use on instead.

addListeners() {
    this.win.on('resize', this.onResize);
}

这篇关于es6类. this.addeventlistener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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