子向父传递参数需要自定义事件
使用
emit(eventName)触发事件
子向父传递参数:
在子组件中个设置一下触发函数,并将参数传给父组件。
export default{
data(){
return{
msg:'Hello from in componentsA ',
}
},
methods:{
onClickme:function(){
this.$emit('child-tell-me-something',this.msg)
}
}
}
在父组件中,$on接受的自定义事件:
<components-a v-on:child-tell-me-something='listenToMyBoy'></components-a>
在methods:
listenToMyBoy:function(msg){
console.log(msg)
//从子组件中传递过来的值msg
this.childWords = msg
},
效果:点击子组件的click事件,将会把子组件msg传给父元素。
细节补充: