子组件向父组件传值——$emit
子组件向父组件传值,通过this.$emit自定义事件
子组件:
<template>
<div> <button v-for="item in btns" :key="item.id" @click="btnClick(item)">{{item.label}}</button> </div> </template> <script> export default { name: '子组件名称', data() { return { btns:[{id:'aaa',label:'按钮1'},{{id:'bbb',label:'按钮2'},{{id:'ccc',label:'按钮3'}] } }, methods:{ btnClick(item){ this.$emit('cpn-click',item) } }
}
</script>
父组件:
<cpn @cpn-click="cpnClick(item)"/>
<script>
import cpn from '子组件路径'
export default {
name: '页面名',
components: { cpn },
data() {
return {
obj:{}
}
},
methosds:{
cpnClick(item){
this.obj=item
}
}
}
</script>