vue父子通信

自定义事件,子传父

子
<template>
    <div>
        <button @click="toParent">点击传到父级</button>
    </div>
</template>
<script>
export default {
    name: 'child',
    methods: {
        toParent () {
            this.$emit('fromChild', 'child')
        }
    }
}
</script>
<template>
    <div>
        <p>子级传过来的值:{
   
   {childVal}}</p>
        <child @fromChild="getChild"></child>
    </div>
</template>
<script>
import child from "@/components/child";
 
export default {
    name: 'parent',
    data () {
        return {
            childVal: ''
        }
    },
    components: {
        child
    },
    methods: {
        getChild (v) {
            this.childVal = v;
        }
    }
}
</script>

猜你喜欢

转载自blog.csdn.net/jvhbi/article/details/108953292