子组件:
<template>
<div >
//触发事件
<div> <button @click="emitToParent">子组件向父组件传值</button></div>
</div>
</template>
<script>
export default {
computed:{
},
created(){
},
components:{},
data(){
return{
data:{
info:'我是子组件像父组件传的值'
}
}
},
methods:{
emitToParent(){
//通过this.$emit触发子元素绑定的自定义事件,并传数据
this.$emit('child-event',this.data)
},
}
}
</script>
<style lang="" scope>
</style>
父组件:
<template>
<Child @child-event="parentReceive">
</Child>
</template>
<script>
import Child from "./components/child";
export default {
components: { Child },
data() {
return {
};
},
methods: {
//接收数据
parentReceive(data) {
console.log(data);
},
}
};
</script>
<style>
</style>