vue父子通讯

父传子

下面是子组件

<template>
<h1>{{ count }}</h1>
</template>
<script>
expose default {
    name:'HelloWorld',
    props: {
        count:{
            type:[String,Number],
            default:"100"
        }    
    }
}


</script>

子组件使用Props发送

父组件接收:

<template>
<div id = "app">
    <HelloWorld count = 100></HelloWorld>
</div>
</template>

<script>
    import HelloWorld from './components/HelloWorld.vue'

    export default {
        name: 'App',
        components: {
            HelloWorld
        },
    }
}
</script>

子传父

eg:每个商品选中后,把价钱传给父组件,然后结算。

<template>
<h1>{{ count }}</h1>
<button @click="handler">按钮</button>
</template>
<script>
expose default {
    name:'HelloWorld',
    props: {
        count:{
            type:[String,Number],
            default:"100"
        }    
    },
    data () {
        return {
        childCount: 0
        }
    },
    methods: {
        handler () {
            this.childCount++,
            this.$emit('child-count-change',this.childCount)
        }
    }
}


</script>

父组件:

<template>
<div id = "app">
    <HelloWorld count = 100,@child-count-change="handler"></HelloWorld>
    <h1>{{ childData }}</h1>
</div>
</template>

<script>
    import HelloWorld from './components/HelloWorld.vue'

    export default {
        name: 'App',
        components: {
            HelloWorld
        },
        data () {
            childData: 0
        }
        methods: {
            handler (childCount) {
                this.childData = childCount
            }
        }
    }

</script>

### Vue3 中父子组件的通讯方式 在 Vue3 的开发过程中,父子组件之间的通信是一个非常重要的部分。以下是详细的说明: #### 1. **父组件向子组件传递数据** 父组件可以通过 `props` 向子组件传递数据。这种方式是单向数据流的一部分,即父组件的数据会向下流动到子组件。 ```html <!-- 父组件 --> <template> <child-component :list="parentList"></child-component> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentList: [ { name: 'Company A', id: 1 }, { name: 'Company B', id: 2 } ] }; } }; </script> ``` ```html <!-- 子组件 --> <template> <div style="border:1px solid #ccc;margin:1em;padding:1em"> <p v-for="item in list" :key="item.id">公司名:{{ item.name }}, ID: {{ item.id }}</p> </div> </template> <script> export default { props: { list: { type: Array, required: true, default: () => [] } } }; </script> ``` 以上代码展示了如何通过 `props` 将父组件的数据传递给子组件[^3]。 --- #### 2. **子组件向父组件传递数据** 子组件可以通过 `$emit` 或者 `defineEmits` 来触发自定义事件,并将数据发送回父组件。父组件则通过监听这些事件来获取子组件的状态或数据。 ```html <!-- 子组件 --> <template> <button @click="sendDataToParent">点击我向父组件发送数据</button> </template> <script> const emit = defineEmits(['updateValue']); export default { methods: { sendDataToParent() { const valueToSend = '来自子组件的消息'; emit('updateValue', valueToSend); } } }; </script> ``` ```html <!-- 父组件 --> <template> <child-component @updateValue="handleUpdate"></child-component> <p>接收到的数据:{{ receivedData }}</p> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { receivedData: '' }; }, methods: { handleUpdate(value) { this.receivedData = value; } } }; </script> ``` 此示例展示了子组件如何通过事件机制通知父组件其状态的变化[^5]。 --- #### 3. **父组件调用子组件的方法** 如果需要从父组件直接调用子组件中的某个方法,则可以使用 `ref` 和 `$refs` 实现这一功能。 ```html <!-- 父组件 --> <template> <child-component ref="myChild"></child-component> <button @click="callChildMethod">调用子组件方法</button> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { callChildMethod() { this.$refs.myChild.someMethod(); } } }; </script> ``` ```html <!-- 子组件 --> <template></template> <script> export default { methods: { someMethod() { console.log('子组件方法被调用了'); } } }; </script> ``` 这段代码显示了如何利用 `ref` 调用子组件内的特定函数[^2]。 --- #### 4. **双向绑定(v-model)** 虽然严格意义上来说这不是真正的父子组件通信模式,但在某些场景下,我们可以借助 `v-model` 创建一种类似于双向绑定的行为。 ```html <!-- 父组件 --> <template> <child-component v-model:title="parentTitle"></child-component> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentTitle: '初始标题' }; } }; </script> ``` ```html <!-- 子组件 --> <template> <input :value="title" @input="$emit('update:title', $event.target.value)" /> </template> <script> export default { props: ['title'] }; </script> ``` 这里实现了基于 `v-model` 的简单双向绑定效果[^4]。 --- #### 总结 Vue3 提供了多种灵活的方式来处理父子组件间的交互需求,无论是简单的属性传递还是复杂的回调逻辑都可以轻松应对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值