当前位置: 首页 > news >正文

v-on=“$listeners“ 这个写法已经废弃了,如进行代替

vue3中v-on="$listeners"发生了什么

在 Vue 3 的虚拟 DOM 中,事件监听器现在只是以 on 为前缀的 attribute,这样它就成为了 $attrs 对象的一部分,因此 $listeners 被移除了

<template>
  <label>
    <input type="text" v-bind="$attrs" />
  </label>
</template>
<script>
export default {
  inheritAttrs: false
}
</script>

如果这个组件接收一个 id attribute 和一个 v-on:close 监听器,那么 $attrs 对象现在将如下所示:

{
  id: 'my-input',
  onClose: () => console.log('close 事件被触发')
}

需要删除所有的 $listeners 用法(vue2切换vue3的时候)

如何进行改造

1. 直接使用 v-on 绑定具体的事件

在 Vue 3 中,可以直接在子组件上使用 v-on 绑定具体的事件,而不是使用 $listeners。这种方式更明确,也更符合 Vue 3 的设计哲学。

<!-- 父组件 -->
<template>
  <ChildComponent
    v-on:child-event="handleChildEvent"
    v-on:another-event="handleAnotherEvent"
  />
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent
  },
  methods: {
    handleChildEvent() {
      console.log('Child event triggered');
    },
    handleAnotherEvent() {
      console.log('Another event triggered');
    }
  }
};
</script>
<!-- 子组件 -->
<template>
  <div>
    <button @click="$emit('child-event')">Trigger Child Event</button>
    <button @click="$emit('another-event')">Trigger Another Event</button>
  </div>
</template>

2. 使用 emits 选项声明事件

Vue 3 引入了 emits 选项,用于显式声明组件可以触发的事件。这有助于类型检查和文档生成。

<!-- 父组件 -->
<template>
  <ChildComponent
    v-on:child-event="handleChildEvent"
    v-on:another-event="handleAnotherEvent"
  />
</template>

<script>
import ChildComponent from './ChildComponent.vue';
export default {
  components: {
    ChildComponent
  },
  emits: ['child-event', 'another-event'],
  methods: {
    handleChildEvent() {
      console.log('Child event triggered');
    },
    handleAnotherEvent() {
      console.log('Another event triggered');
    }
  }
};
</script>
<!-- 子组件 -->
<script>
export default {
  emits: ['child-event', 'another-event']
};
</script>

<template>
  <div>
    <button @click="$emit('child-event')">Trigger Child Event</button>
    <button @click="$emit('another-event')">Trigger Another Event</button>
  </div>
</template>

3、使用组合式 API (Composition API)

如果你使用的是 Vue 3 的组合式 API,可以通过 emits 和 defineEmits 来处理事件。

<!-- 父组件 -->
<template>
  <ChildComponent
    v-on:child-event="handleChildEvent"
    v-on:another-event="handleAnotherEvent"
  />
</template>

<script setup>
import ChildComponent from './ChildComponent.vue';

const handleChildEvent = () => {
  console.log('Child event triggered');
};

const handleAnotherEvent = () => {
  console.log('Another event triggered');
};
</script>
<!-- 子组件 -->
<script setup>
const emit = defineEmits(['child-event', 'another-event']);
</script>

<template>
  <div>
    <button @click="emit('child-event')">Trigger Child Event</button>
    <button @click="emit('another-event')">Trigger Another Event</button>
  </div>
</template>

4、直接使用 $attrs 绑定事件监听器

在 Vue 3 中,$listeners 已经被并入 $attrs,所以你可以直接使用 $attrs 来绑定事件监听器。

<!-- 父组件 -->
<template>
  <ChildComponent
    @child-event="handleChildEvent"
    @another-event="handleAnotherEvent"
  />
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent
  },
  methods: {
    handleChildEvent() {
      console.log('Child event triggered');
    },
    handleAnotherEvent() {
      console.log('Another event triggered');
    }
  }
};
</script>
<!-- 子组件 -->
<template>
  <div v-bind="$attrs">
    <button @click="$emit('child-event')">Trigger Child Event</button>
    <button @click="$emit('another-event')">Trigger Another Event</button>
  </div>
</template>

<script>
export default {
  inheritAttrs: false // 可选,如果不希望根元素自动接收 $attrs
};
</script>

相关文章:

  • 指令系统1(数据传输指令)
  • 电子工程师转战汽车OEM主机厂之路
  • (保姆级教程)CAN总线—如何使用CANoe(VN1640)的Scaner功能测量样件的波特率
  • 用于 RGB-D 显著目标检测的点感知交互和 CNN 诱导的细化网络(问题)
  • Metasploit Framework(MSF)使用教程与命令详解
  • bluetooth与hciconfig的区别
  • 使用bat批量获取WORD中包含对应字符的段落,段落使用回车换行
  • 1221. 四平方和 -蓝桥杯真题-哈希函数思想
  • 在 React 中使用 Hooks 从服务端获取数据的完整指南
  • 网络安全之前端学习(HTML属性篇)
  • 力扣刷题46. 全排列
  • HTML5前端第四章节
  • RG-S3760应用协议配置
  • 动静态库的使用和原理(下)
  • 区块链(Blockchain)
  • GED-VIZ部署解决方案
  • Java学习打卡-Day19-Set、HashSet、LinkedHashSet
  • Deepseek+扣子实现xhs内容自动采集
  • 云原生服务网格:微服务通讯的量子纠缠革命
  • ICLR 2025 机器人智能灵巧操作更进一步DexTrack
  • 法学联合书单|法庭上的妇女
  • 将人工智能送上太空,我国太空计算卫星星座成功发射
  • 国务院关税税则委:调整对原产于美国的进口商品加征关税措施
  • 融创中国:境外债务重组计划聆讯定于9月15日召开
  • 有关部门负责人就《新时代的中国国家安全》白皮书答记者问
  • 来伊份:已下架涉事批次蜜枣粽产品,消费者可获额外补偿,取得实物后进一步分析