作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现一些功能,以在用户单击菜单/模式区域之外时关闭所有菜单/模式。
this.closeMenu()
已正确调用,但我收到一条错误,提示 this.closeMenu 不是函数
。这是什么原因?
methods: Object.assign({},
{
closeMenu(){
console.log("close menu")
}
}
)
mounted(){
$(document).on('click', function(event) {
if (!$(event.target).closest('.menu').length){
// close all menus
this.closeMenu()
}
});
}
最佳答案
这里的
this
关键字不是 Vue
实例。您可以通过分配 var self = this
mounted(){
var self = this;
$(document).on('click', function(event) {
if (!$(event.target).closest('.menu').length){
// close all menus
self.closeMenu()
}
});
}
关于javascript - 错误: not a function (on mounted),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51644419/
我是一名优秀的程序员,十分优秀!