Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VPopup 中绑定的监听器没有绑定 Vue 实例(即 this #4264

Closed
timongh opened this issue Jun 22, 2023 · 0 comments
Closed

VPopup 中绑定的监听器没有绑定 Vue 实例(即 this #4264

timongh opened this issue Jun 22, 2023 · 0 comments
Labels
bug bug

Comments

@timongh
Copy link
Contributor

timongh commented Jun 22, 2023

问题描述

if (this.open) {
document.documentElement.addEventListener(type, this.openHandler)
} else {
document.documentElement.removeEventListener(type, this.openHandler)
}

这里绑定的是 this.openHandler,而这种写法并没有将当前的 this 捕获到函数里。因此实际 openHandler 执行时,其访问到的 this 是错误的对象。

可用的写法:

(e: Event) => this.openHandler(e)
this.openHandler.bind(this)
lodash.bind(this.openHandler, this)
@the1812 the1812 added the bug bug label Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug bug
Projects
None yet
Development

No branches or pull requests

2 participants