方式一
1.子组件
<p @click="a">某个事件</p>
methods:{
a(){
// b是子组件自定义的事件名
this.$emit('b','定义传给父组件的值')
}
}
2.父组件调用
父组件定义
<A @b = "b"></A>
methods:{
a(e){
console.log(e) // 定义传给父组件的值
},
},
方式二
1.子组件
子组件
<div @click="c"></div>
methods:{
c(){
this.$emit('b','给父组件传去的值')
},
},
2.父组件
父组件
<A ref="a"></A>
//挂载
mounted(){
// this.$refs.a获取真实的DOM元素
/ $on可以理解为当....时候 $on('b')当b事件被触发的时候
this.$refs.a.$on('b',this.c)
// 下面这个只能触发一次
// this.$refs.a.$once('b',this.c)
}
methods:{
c(e){
console.log('子组件传来的值',e)
}
}