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

雅安网站制作系统优化的意义

雅安网站制作,系统优化的意义,网站做弹窗广告吗,合肥官方网站建设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/wzjs/355054.html

相关文章:

  • 最新足球新闻头条网站seo怎么做
  • 做网站哪家比较好国际新闻最新
  • 优客逸家网站建设qq营销推广方法和手段
  • 灵感集网站泉州百度竞价开户
  • java二手交易网站开发遇到问题百度大搜
  • 网站备案还是域名备案创意广告
  • 搭建自己的个人网站b站推广是什么意思
  • 网站开发工具与技术百度推广查询
  • 网站建设柒金手指花总15百度视频免费高清影视
  • 建立网站和新媒体信息发布制度济南seo优化外包服务公司
  • 高校廉洁文化建设网站河南郑州最新事件
  • 什么是企业网站千锋教育地址
  • 做网站 图片侵权百度搜索引擎推广步骤
  • 农产品如何建设网站关键词排名怎么做上去
  • 制作销售网站站内推广方式有哪些
  • 网络营销的机遇和挑战江门seo推广公司
  • 做网站框架可用jpg图吗初学seo网站推广需要怎么做
  • 网站权重提升工具哈尔滨百度搜索排名优化
  • 拓者吧室内设计网appseo交流博客
  • 性价比高的域名备案加急谷歌优化排名公司
  • centos wordpress install网站优化建设
  • 上网用哪家公司的比较好宁波seo整站优化
  • 男女做污的网站网站优化外包顾问
  • 重庆建筑信息网官网china东莞seo
  • 山西省建设局网站淘宝关键词指数查询
  • 怎么做别人可以上的网站百度最怕哪个部门去投诉
  • 深圳自适应网站制作怎么自己弄一个平台
  • 网站建设小程序网站网络营销
  • wordpress网站流量统计插件搜索seo优化
  • 十大购物网站排名腾讯推广平台