在 Vue 中,通过 $on 方法动态添加的事件监听器,需要使用 $off 方法来移除。核心原则是:$off 的参数必须与 $on 完全匹配(事件名和回调函数引用一致)。以下是具体实现方法及场景示例:
$on 用于绑定事件,$off 用于移除,两者参数对应关系如下:
这是常用的场景,需确保 $off 的回调函数与 $on 引用一致(不能用匿名函数)。
<script>
export default {
created() {
// 1. 定义具名回调函数(确保引用唯一)
this.handleClick = () => {
console.log('自定义事件被触发');
};
// 2. 用 $on 绑定事件
this.$on('custom-click', this.handleClick);
},
mounted() {
// 模拟事件触发
this.$emit('custom-click'); // 输出:自定义事件被触发
},
beforeDestroy() {
// 3. 用 $off 移除指定事件的指定回调(关键步骤)
this.$off('custom-click', this.handleClick);
}
};
</script>
若一个事件绑定了多个回调,可通过 $off('eventName') 一次性移除所有。
<script>
export default {
created() {
// 绑定多个回调到同一事件
this.$on('custom-event', () => console.log('回调1'));
this.$on('custom-event', () => console.log('回调2'));
},
mounted() {
this.$emit('custom-event'); // 输出:回调1、回调2
},
beforeDestroy() {
// 移除 custom-event 的所有回调
this.$off('custom-event');
}
};
</script>
若需清空组件上所有通过 $on 绑定的事件,直接调用 $off() 即可(无参数)。
<script>
export default {
created() {
this.$on('event1', () => {});
this.$on('event2', () => {});
},
beforeDestroy() {
// 移除所有事件的所有回调
this.$off();
}
};
</script>
父组件通过 v-on 绑定子组件的自定义事件,本质是子组件内部通过 $on 处理,子组件可在卸载时用 $off 移除。
<!-- 子组件 Child.vue -->
<script>
export default {
created() {
// 父组件绑定的 @child-event 会被 Vue 内部转为 $on
// 子组件可通过 $off 移除(需知道事件名和回调)
this.$on('child-event', this.handleChildEvent);
},
methods: {
handleChildEvent() {
console.log('子组件事件触发');
}
},
beforeDestroy() {
this.$off('child-event', this.handleChildEvent);
}
};
</script>
$on 若使用匿名函数绑定,$off 无法找到相同引用,导致移除失败。
this.$on('custom-event', () => console.log('匿名回调'));
this.$off('custom-event', () => console.log('匿名回调'));
this.handleEvent = () => console.log('具名回调');
this.$on('custom-event', this.handleEvent);
this.$off('custom-event', this.handleEvent);
若 $on 绑定了多个回调,$off('eventName', handler) 仅移除指定回调,其他回调仍有效;若需移除所有,需用 $off('eventName')。
Vue 组件卸载时,会自动移除所有通过 $on 绑定的事件监听器,因此若仅需在组件销毁时移除,无需手动调用 $off。但如果组件未销毁(如隐藏而非销毁),则需手动移除。
$on / $off 绑定的是组件实例的事件,而非 DOM 事件。若需移除 DOM 事件,仍需用 removeEventListener。
移除 $on 动态添加的事件监听器,核心是:
- 用
$off 方法,参数与 $on 完全匹配(事件名 + 回调函数引用);
- 避免使用匿名函数,改用具名函数确保引用一致;
- 组件卸载时 Vue 会自动清理,无需手动
$off(除非组件未销毁);
- 区分组件实例事件(
$on / $off)和 DOM 事件(addEventListener / removeEventListener)。
|