gpt4 book ai didi

javascript - 错误: not a function (on mounted)

转载 作者:行者123 更新时间:2023-12-03 01:23:32 25 4
gpt4 key购买 nike

我正在尝试实现一些功能,以在用户单击菜单/模式区域之外时关闭所有菜单/模式。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com