Vue中,如果我们希望直接给子组件添加一个原生事件,就得使用
.native
修饰符,如同:@click.native="...xxx"
。当我们希望组件绑定事件的时候更加优雅以及不使用.native
修饰符的时候,就可以使用如下方法
1. 添加原生事件
1.1 父组件
<template>
<div>
点击事件
<demo-button @click="change"></demo-button>
</div>
</template>
<script>
import demoButton form 'xxx'
export default {
components:{
demoButton
},
methods:{
change(e) {
console.log("点击了",e)
}
}
}
</script>
1.2 子组件
<template>
<div>
<button @click="clickBtn"></button>
</div>
</template>
<script>
export default{
methods:{
clickbtn(e){
// 触发父组件传下来的自定义事件 click
this.$emit('click',e)
}
}
}
</script>
1.3 注意
- 该方法不止针对于
click
事件,例如change
等事件也是有效的