之前写过一篇文章,vue中父组件添加自定义参数后,如何获取子组件$emit传递过来的参数
现在已经进入vue3.x开发的时代了,那么vue3.x中父组件添加自定义参数后,如何获取子组件$emit传递过来的参数?
1、子组件使用emit传过来的参数只有一个时(父组件可以使用$event接收子组件传过来的参数)
子组件(child1):
this.$emit('fromChild1','我是子组件传过来的参数--child1')
父组件:
<child1 @fromChild1="($event) => getChild1($event,'我是父组件中的自定义参数--parent1')"></child1>
getChild1($event,parameter){
console.log($event); // 我是子组件传过来的参数--child1
console.log(parameter); // 我是父组件中的自定义参数--parent1
},
console.log打印结果: 我是子组件传过来的参数--child1
我是父组件中的自定义参数--parent1
大家可以对比一下vue3和vue2的写法,看看有什么不同点。