09-组件通信-父传子(props的驼峰标识)

09-组件通信-父传子(props的驼峰标识)

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>组件通信-父传子(props的驼峰标识)</title>
</head>

<body>
    <div id="app">
        <!-- 当props中用驼峰命名法时如cInfo,绑定时要改为v-info -->
        <cpn :c-info="info"></cpn>
    </div>

    <template id="cpn">
        <div>
            <h2>{{cInfo}}</h2>
            <h2>{{childMessageAge}}</h2>
        </div>

    </template>


    <script src="../js/vue.js"></script>

    <script>
        // root组件
        const cpn = {
            template: "#cpn",
            props: {
                cInfo: {
                    type: Object,
                    default() {
                        return {}
                    }
                },
                childMessageAge: {
                    type: String,
                    default: 'aaa'
                }
            }
        }



        const app = new Vue({
            el: "#app",
            data: {
                info: {
                    name: "coderwhy",
                    age: 19,
                    height: 1.99
                }

            },
            components: {
                cpn
            }
        })
    </script>
</body>

</html>
### Vue3 组件通信方法 #### 子组件组件传递数据 在 Vue3 中,子组件可以通过 `emit` 方法向组件发送事件并附带数据。具体实现如下: 1. **子组件定义要传递的数据** 在子组件的 `data()` 函数中定义需要传递给组件的数据变量 `transmsg`[^1]。 ```javascript export default { data() { return { transmsg: '我是子组件传递的值' }; }, methods: { sendMessageToParent() { this.$emit('custom-event', this.transmsg); // 发送自定义事件 custom-event 并携带数据 } } }; ``` 2. **组件监听子组件发出的事件** 组件通过绑定子组件上的自定义事件来接收来自子组件的消息。 ```html <template> <ChildComponent @custom-event="handleCustomEvent" /> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { handleCustomEvent(message) { console.log('接收到子组件消息:', message); } } }; </script> ``` --- #### 组件向子组件传递数据 组件可以利用 Props 将数据传递到子组件中。Props 是一种单向下行的数据流机制。 1. **组件传递数据至子组件** 组件通过属性的方式将数据传入子组件。 ```html <template> <ChildComponent :parentMessage="messageFromParent" /> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { messageFromParent: '这是来自组件的消息' }; } }; </script> ``` 2. **子组件接收 Prop 数据** 子组件声明接受的 Prop 属性,并将其用于模板或其他逻辑中。 ```javascript export default { props: ['parentMessage'], // 声明 prop mounted() { console.log('接收到组件消息:', this.parentMessage); } }; ``` --- #### 组件访问子组件中的 Data 数据 如果组件需要直接获取子组件内部的状态(不推荐频繁使用),可以借助 `$refs` 实现[^2]。 1. **子组件暴露特定的方法或状态** 使用 `defineExpose` 显式暴露子组件的内容(Vue3 Composition API 特性)。 ```javascript <script setup> import { ref } from 'vue'; const childData = ref('我是子组件的数据'); defineExpose({ childData }); </script> ``` 2. **组件通过 $refs 访问子组件数据** 组件可以在挂载完成后通过 `$refs` 获取子组件实例及其公开内容。 ```html <template> <ChildComponent ref="childRef" /> <button @click="accessChildData">访问子组件数据</button> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { accessChildData() { console.log(this.$refs.childRef.childData); // 输出子组件暴露的数据 } } }; </script> ``` --- ### 总结 -组件组件传递数据主要依赖于 `$emit` 和自定义事件。 - 组件向子组件传递数据则通过 Props 完成。 - 如果确实需要跨边界访问子组件内部状态,则可采用 `$refs` 或显式暴露接口的方式完成交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林笙10

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值