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

拟采用建站技术长沙最新死亡事件

拟采用建站技术,长沙最新死亡事件,网络工程师证书考试时间,长沙正规关键词优化价格从优vue3中v-on"$listeners"发生了什么 在 Vue 3 的虚拟 DOM 中&#xff0c;事件监听器现在只是以 on 为前缀的 attribute&#xff0c;这样它就成为了 $attrs 对象的一部分&#xff0c;因此 $listeners 被移除了。 <template><label><input type"t…

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><ChildComponentv-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><ChildComponentv-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><ChildComponentv-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>
http://www.dtcms.com/a/585656.html

相关文章:

  • Java面向对象三大特性详解:封装、继承、多态与接口
  • 力扣(LeetCode) ——209. 长度最小的子数组(C++)
  • tcp关闭的四次挥手
  • 基于HAL库实现CAN通信RS485通信WIFI模块和4G模块的AT指令和TCP透传
  • 采购网站有哪些东莞百度推广优化排名
  • 黄石网站设计公司网站都必须要备案吗
  • 36.华为云计算类服务实践
  • 长沙网站制作合作商网站更改文章标题
  • 【赵渝强老师】使用Helm简化Kubernetes(K8s)应用的部署和管理
  • 网页设计与制作教程期末考试东莞网络优化公司排名
  • docker打包,启动java程序
  • Debian 13 安装 Kubernetes 最新版 并使用 containerd 运行时
  • 通过操作地址,来进行STM32的写入GPIO端口值
  • 压力测试详解
  • 网站 怎么备案佛山营销型网站设计
  • 网站关键字被百度收录外链群发
  • Qt点击信号全解析:QTreeWidget交互指南
  • C++ 头文件:语言功能的 “模块化工具箱”(第三章)
  • 系统架构设计师学习大纲目录
  • 网站建设百度推广说词网站服务器在本地是指
  • Highcharts 3D图:深入解析与实际应用
  • 全景图制作工具( 10 种)
  • 虚拟位置映射(标签鸽
  • Pytorch 学习之Transforms
  • 中国建设银行网站的社保板块在哪里徐州专业网站制作公司
  • 免费建立移动网站吗模拟百度搜索词进入网站
  • Windows 如何安装 Chocolatey
  • 江苏高端网站建设一个网站怎么赚钱
  • 鸿蒙 ArkTS 入门教程:小白实战 List 列表开发(详解 @State, ForEach, @Builder)
  • Redis_8_List