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

歡迎來(lái)到合肥浪訊網(wǎng)絡(luò)科技有限公司官網(wǎng)
  咨詢(xún)服務(wù)熱線:400-099-8848

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

發(fā)布時(shí)間:2025-11-04 文章來(lái)源:本站  瀏覽次數(shù):799
在 Vue 中移除多個(gè)事件監(jiān)聽(tīng)器,核心思路是 “集中管理事件與回調(diào)的關(guān)聯(lián)關(guān)系,在合適時(shí)機(jī)批量遍歷移除”,避免遺漏或重復(fù)操作。以下是具體實(shí)現(xiàn)方法,覆蓋 Vue 3 和 Vue 2 場(chǎng)景:

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

1. 用數(shù)組批量管理(適合同類(lèi)型事件)

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 管理(適合多類(lèi)型 / 多元素)

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(選項(xiàng)式 API):在 beforeDestroy 中批量移除

javascript
運(yùn)行
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);
    });
  }
};

三、第三方庫(kù)事件的批量移除

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

總結(jié)

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

上一條:在Vue中,如何移除一個(gè)...

下一條:讓網(wǎng)站走向成功的五大內(nèi)容...

盐津县| 颍上县| 昌图县| 理塘县| 濮阳市| 屏南县| 辽阳市| 关岭| 茶陵县| 梨树县| 那坡县| 新安县| 祁连县| 晋中市| 英山县| 建宁县| 万载县| 田林县| 仁寿县| 盐源县| 西宁市| 西吉县| 潞城市| 泰来县| 且末县| 城固县| 河西区| 靖远县| 永昌县| 黔江区| 呼和浩特市| 赤城县| 贡山| 徐闻县| 淮北市| 文山县| 来安县| 株洲市| 大宁县| 克东县| 吉林市|