methods: {
clickOther: function () {};
}
mounted() {
window.addEventListener("click", this.clickOther);
}
beforeDestroy() { // 实例销毁之前对点击事件进行解绑
window.removeEventListener('click', this.clickOther);
}
本文详细介绍了在Vue中如何使用methods定义事件处理函数,并在组件挂载时通过mounted生命周期钩子添加事件监听器。同时,阐述了在组件销毁前如何正确地移除事件监听,避免内存泄漏。
methods: {
clickOther: function () {};
}
mounted() {
window.addEventListener("click", this.clickOther);
}
beforeDestroy() { // 实例销毁之前对点击事件进行解绑
window.removeEventListener('click', this.clickOther);
}
466
4366
3080

被折叠的 条评论
为什么被折叠?