v-on.native 修饰符已移除 重大变更
概述
v-on 的 .native 修饰符已移除。
2.x 语法
传递给组件的 v-on 事件监听器默认情况下仅在使用 this.$emit 发射事件时触发。要改为向子组件的根元素添加本机 DOM 监听器,可以使用 .native 修饰符
html
<my-component
v-on:close="handleComponentEvent"
v-on:click.native="handleNativeClickEvent"
/>3.x 语法
v-on 的 .native 修饰符已移除。同时,新的 emits 选项 允许子组件定义它确实发射的事件。
因此,Vue 现在将所有未定义为子组件发射事件的事件监听器添加为子组件根元素的本机事件监听器(除非在子组件的选项中设置了 inheritAttrs: false)。
html
<my-component
v-on:close="handleComponentEvent"
v-on:click="handleNativeClickEvent"
/>MyComponent.vue
html
<script>
export default {
emits: ['close']
}
</script>迁移策略
- 移除所有
.native修饰符的实例。 - 确保所有组件都使用
emits选项记录其事件。