Vue 3 迁移策略笔记—— 第9节:新增 emits 选项

编程入门 行业动态 更新时间:2024-10-23 22:31:35

Vue 3 迁移策略笔记—— 第9节:新增 emits <a href=https://www.elefans.com/category/jswz/34/1771211.html style=选项"/>

Vue 3 迁移策略笔记—— 第9节:新增 emits 选项

前言

本笔记主要基于官方文档《迁移策略—— emits 选项》汇总而来。如有理解出入,请以官方文档为主。建议您以官方文档为主,本文为辅。这样您可以“以自己为主”审视的阅读,从而不被我的观点带偏。

概述

Vue 3.x 新增了一个emits 组件选项,用来定义组件可以向父组件 emit 的事件。其类似于 props

Vue 2.x 的 emit

在 Vue 2.x , 我们可以定义组件接收的props,但是我们不能声明组件可以emit哪些event

<template><div><p>{{ text }}</p><button v-on:click="$emit('accepted')">OK</button></div>
</template>
<script>export default {props: ['text']}
</script>

Vue 3.x 的 emit

到了 Vue 3.x ,组件所有 emit 的事件必须要在emits 选项中声明。

<template><div><p>{{ text }}</p><button v-on:click="$emit('accepted')">OK</button></div>
</template>
<script>export default {props: ['text'],emits: ['accepted']}
</script>

此外,emits 可以接收一个对象。每个属性的值可以为空,也可以为验证器函数。

<template><div><p>{{ text }}</p><button v-on:click="$emit('click')">click</button><button v-on:click="$emit('submit')">submit</button></div>
</template>
<script>export default {props: ['text'],emits: {// no validationclick: null,// with validationsubmit: payload => {if (payload.email && payload.password) {return true} else {console.warn(`Invalid submit event payload!`)return false}}}}
</script>

强烈建议组件中使用的所有通过emit触发的event都在emits中声明。

因为Vue 3.x 中移除的.native修饰符。任何没有在emits中进行声明的event,将会自动被添加到$attrs。而attrs默认情况下是绑定到根组件的。

英文原文:

It is highly recommended that you document all of the events emitted by each of your components using emits.

This is especially important because of the removal of the .native modifier. Any listeners for events that aren’t declared with emits will now be included in the component’s $attrs, which by default will be bound to the component’s root node.

这样就会造成,如果emit的是原生的事件(如,click),就会存在两次触发。如:

<template><button v-on:click="$emit('click', $event)">OK</button>
</template>
<script>
export default {emits: [] // without declared event
}
</script>
  • 一次来自于$emit的触发;

  • 一次来自于根元素原生事件监听器的触发;


本系列目录

  • Vue 3 迁移策略笔记—— 第1节:v-for 中的 Ref 数组

  • Vue 3 迁移策略笔记—— 第2节:Async Components 异步组件

  • Vue 3 迁移策略笔记—— 第3节:Attribute Coercion Behavior (属性强制行为)

  • Vue 3 迁移策略笔记——第4节:$attrs 包括class&style

  • Vue 3 迁移策略笔记—— 第5节:移除 $children

  • Vue 3 迁移策略笔记—— 第6节:自定义指令

  • Vue 3 迁移策略笔记—— 第7节:自定义元素交互

  • Vue 3 迁移策略笔记—— 第8节:Data 选项

  • Vue 3 迁移策略笔记—— 第9节:新增 emits 选项

  • Vue 3 迁移策略笔记—— 第10节:事件 API

  • Vue 3 迁移策略笔记—— 第11节:移除过滤器

  • Vue 3 迁移策略笔记—— 第12节:片段

  • Vue 3 迁移策略笔记—— 第13节:函数式组件

  • Vue 3 迁移策略笔记—— 第14节:全局 API

  • Vue 3 迁移策略笔记—— 第15节:全局 API 的 tree shaking

  • Vue 3 迁移策略笔记—— 第16节:Inline Template 属性

  • Vue 3 迁移策略笔记—— 第17节:Key 属性

  • Vue 3 迁移策略笔记—— 第18节:按键修饰符

  • Vue 3 迁移策略笔记—— 第19节:移除 $listeners

  • Vue 3 迁移策略笔记—— 第20节:Props 的默认值函数不能访问this

  • Vue 3 迁移策略笔记—— 第21节:渲染函数 API

  • Vue 3 迁移策略笔记—— 第22节:Slots 的统一

  • Vue 3 迁移策略笔记—— 第23节:Transition Class 的变化

  • Vue 3 迁移策略笔记—— 第24节:Transition Group 不再需要设置根元素

  • Vue 3 迁移策略笔记—— 第25节:v-on.native修饰符被移除

  • Vue 3 迁移策略笔记—— 第26节:在组件上使用 v-model 的变化

  • Vue 3 迁移策略笔记—— 第27节:v-if 和 v-for 的优先级

  • Vue 3 迁移策略笔记—— 第28节:v-bind 合并行为

  • Vue 3 迁移策略笔记—— 第29节:数组的监听

更多推荐

Vue 3 迁移策略笔记—— 第9节:新增 emits 选项

本文发布于:2024-03-13 02:52:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1733023.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   策略   笔记   Vue   emits

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!