Vue3默认将所有以on开头的函数名当作v-on事件绑定,这是真的吗?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1186个文字,预计阅读时间需要5分钟。
最近重新阅读了vue3的RFCs,发现一个细节:以on开头的props会被当作v-on绑定处理,on后面的部分会被转换为全小写作为事件名(更多细节见下文)。也就是说,之后可以这样写。
最近在重新看vue3的rfcs,发现一个细节,原话如下:
props that start with on are handled as v-on bindings, with everything after on being converted to all-lowercase as the event name (more on this below)
也就是说,以后如果你在传递props的时候,以 on 开头的props,如果在组件上没有做props的声明,那么会被当作事件绑定到组件的根节点上。
究其原因,我大致概括了两点:
- 兼容vue2中的v-on.native
- vue3的vnode声明把props拍平了,为了区分事件和其他props,就统一把所有的on开通的props默认作为事件绑定
为此,我开了一个issue来讨论这个问题,issue地址 。
本文共计1186个文字,预计阅读时间需要5分钟。
最近重新阅读了vue3的RFCs,发现一个细节:以on开头的props会被当作v-on绑定处理,on后面的部分会被转换为全小写作为事件名(更多细节见下文)。也就是说,之后可以这样写。
最近在重新看vue3的rfcs,发现一个细节,原话如下:
props that start with on are handled as v-on bindings, with everything after on being converted to all-lowercase as the event name (more on this below)
也就是说,以后如果你在传递props的时候,以 on 开头的props,如果在组件上没有做props的声明,那么会被当作事件绑定到组件的根节点上。
究其原因,我大致概括了两点:
- 兼容vue2中的v-on.native
- vue3的vnode声明把props拍平了,为了区分事件和其他props,就统一把所有的on开通的props默认作为事件绑定
为此,我开了一个issue来讨论这个问题,issue地址 。

