国产人成视频在线观看,青青操视频在线观看国产,国产精品大秀视频日韩精品,人妻丰满熟妇岳av无码区hd

歡迎來到合肥浪訊網絡科技有限公司官網
  咨詢服務熱線:400-099-8848

在Vue中,如何移除多個事件監(jiān)聽器?

發(fā)布時間:2025-11-04 文章來源:本站  瀏覽次數:863
在 Vue 中移除多個事件監(jiān)聽器,核心思路是 “集中管理事件與回調的關聯關系,在合適時機批量遍歷移除”,避免遺漏或重復操作。以下是具體實現方法,覆蓋 Vue 3 和 Vue 2 場景:

一、Vue 3(組合式 API):用數組 / 對象管理事件

1. 用數組批量管理(適合同類型事件)

vue
<script setup>
import { onMounted, onUnmounted, ref } from 'vue';

const btnRef = ref(null);
const events = [
  { type: 'click', handler: handleClick },
  { type: 'mouseenter', handler: handleMouseEnter }
];

function handleClick() { /* ... */ }
function handleMouseEnter() { /* ... */ }

onMounted(() => {
  events.forEach(({ type, handler }) => {
    btnRef.value?.addEventListener(type, handler);
  });
});

onUnmounted(() => {
  events.forEach(({ type, handler }) => {
    btnRef.value?.removeEventListener(type, handler);
  });
});
</script>

2. 用 Map 管理(適合多類型 / 多元素)

vue
<script setup>
import { onMounted, onUnmounted, ref } from 'vue';

const boxRef = ref(null);
const eventMap = new Map([
  ['mousedown', handleMouseDown],
  ['mousemove', handleMouseMove],
  ['mouseup', handleMouseUp]
]);

function handleMouseDown() { /* ... */ }
function handleMouseMove() { /* ... */ }
function handleMouseUp() { /* ... */ }

onMounted(() => {
  eventMap.forEach((handler, type) => {
    boxRef.value?.addEventListener(type, handler);
  });
});

onUnmounted(() => {
  eventMap.forEach((handler, type) => {
    boxRef.value?.removeEventListener(type, handler);
  });
});
</script>

二、Vue 2(選項式 API):在 beforeDestroy 中批量移除

javascript
運行
export default {
  data() {
    return {
      eventListeners: [
        { el: 'window', type: 'scroll', handler: this.handleScroll },
        { el: 'document', type: 'click', handler: this.handleClick }
      ]
    };
  },
  methods: {
    handleScroll() { /* ... */ },
    handleClick() { /* ... */ }
  },
  mounted() {
    this.eventListeners.forEach(({ el, type, handler }) => {
      const target = el === 'window' ? window : document;
      target.addEventListener(type, handler);
    });
  },
  beforeDestroy() {
    this.eventListeners.forEach(({ el, type, handler }) => {
      const target = el === 'window' ? window : document;
      target.removeEventListener(type, handler);
    });
  }
};

三、第三方庫事件的批量移除

vue
<script setup>
import { onMounted, onUnmounted, ref } from 'vue';
import * as echarts from 'echarts';

const chartRef = ref(null);
let chart = null;

// ECharts 事件映射
const chartEvents = [
  ['click', handleChartClick],
  ['legendselectchanged', handleLegendChange]
];

function handleChartClick() { /* ... */ }
function handleLegendChange() { /* ... */ }

onMounted(() => {
  chart = echarts.init(chartRef.value);
  chartEvents.forEach(([type, handler]) => {
    chart.on(type, handler);
  });
});

onUnmounted(() => {
  chartEvents.forEach(([type, handler]) => {
    chart.off(type, handler);
  });
  chart.dispose();
});
</script>

四、避坑指南

  1. 確;卣{函數引用一致
    • 錯誤:用匿名函數綁定
    • 正確:用具名函數或 useCallback 緩存
  2. 捕獲階段參數匹配
    javascript
    運行
    // 綁定
    el.addEventListener('click', handler, true);
    // 移除
    el.removeEventListener('click', handler, true);
    
  3. 動態(tài)元素的事件委托 對 v-for 列表,優(yōu)先用事件委托減少監(jiān)聽:
    vue
    <ul @click="handleItemClick">
      <li v-for="item in list" :data-id="item.id">{{ item.name }}</li>
    </ul>
    

總結

  • 管理方式:用數組 / Map 存儲事件配置,集中管理
  • 移除時機:Vue 3 在 onUnmounted,Vue 2 在 beforeDestroy
  • 核心原則:綁定與移除的參數(類型、回調、捕獲階段)必須完全一致

上一條:在Vue中,如何移除一個...

下一條:讓網站走向成功的五大內容...

山西省| 宁阳县| 同心县| 广东省| 红河县| 贡嘎县| 平山县| 丽江市| 岳普湖县| 寻甸| 张家川| 红原县| 濉溪县| 绵竹市| 无棣县| 西平县| 西乌| 丰宁| 科技| 南昌市| 丰都县| 松阳县| 大新县| 腾冲县| 崇明县| 原阳县| 乌苏市| 武功县| 太仆寺旗| 鸡泽县| 曲松县| 双鸭山市| 武清区| 弋阳县| 界首市| 聂荣县| 濮阳县| 海丰县| 鹤峰县| 西丰县| 皋兰县|