使用 $emit 从子组件传递数据到父组件时,主要有以下3类情况
// child组件,在子组件中触发事件 this.$emit('handleFather', '子参数1','子参数2','子参数3') // father组件,在父组件中引用子组件 <child @handleFather="handleFather"></child> <script> export default { components: { child, } methods: { handleFather(param1,param2,param3) { console.log(param) // } } } </script>
解析:
// child组件,在子组件中触发事件并传多个参数 this.$emit('handleFather', param1, param2,) //father组件,在父组件中引用子组件 <child @handleFather="handleFather(arguments)"></child> <script> export default { components: { child, } methods: { handleFather(param) { console.log(param[0]) //获取param1的值 console.log(param[1]) //获取param2的值 } } } </script>
解析:
写法一:
// child组件,在子组件中触发事件 this.$emit('handleFather', '子参数对象') //father组件,在父组件中引用子组件 <child @handleFather="handleFather($event, fatherParam)"></child> <script> export default { components: { child, } methods: { handleFather(childObj, fatherParam) { console.log(childObj) // 打印子组件参数(对象) console.log(fatherParam) // 父组件参数 } } } </script>
写法二:
// child组件,在子组件中触发事件并传多个参数 this.$emit('handleFather', param1, param2,) //father组件,在父组件中引用子组件 <child @handleFather="handleFather(arguments, fatherParam)"></child> <script> export default { components: { child, } methods: { handleFather(childParam, fatherParam) { console.log(childParam) //获取arguments数组参数 console.log(fatherParam) //获取fatherParam } } } </script>
总结:
到此这篇关于vue中$emit传递多个参(arguments和$event)的文章就介绍到这了,更多相关vue $emit传递多个参内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!