组件通信-ref链传输数据

本文详细介绍了Vue.js中父子组件间的数据传递与事件触发机制,包括如何使用ref链实现子组件向父组件传递数据,以及父组件如何将数据传给子组件。同时,通过实例展示了数据绑定、事件处理及属性传递的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(通过ref链 实现子传父—>父传子)

	<div id="app">
        <Father></Father>
    </div>
    <template id="father">
        <!-- 组件中根元素必须唯一 -->
        <div>
            <h3> 这里是father </h3>
(7.显示父组件的值)
            <button @click='transport'>点击</button>
            <p>Father的n {{ n }} </p>
(1.绑定ref链)
            <Msga ref='msgaa'></Msga>
(4.获取兄弟组件的数据)-(8.:abc="n"把数据传送给msgb)
            <Msgb ref='msgbb':abc="n"></Msgb>
        </div>
    </template>
    <template id="msga">
        <div>
            <h3> 这里是 msg1 </h3>
        </div>
    </template>

    <template id="msgb">
        <div>
            <h3> 这里是msg2 </h3>
(10.绑定事件)
			<button @click="output">输出数据</button>
        </div>
    </template>
_____________________________________________________________________________
    Vue.component('Father', {
        template: '#father',
(5.将数据传给父组件)
        data() {
            return {
                n: 0
            }
        },
(6.父组件保存数据的值)
       	methods: {
            transport() {
                this.n = this.$refs.msgaa.content
            }
        }
    })

    Vue.component('Msga', {
        template: '#msga',
(2.数据传输)
        data(){
            return{
                content:数据
            }
        }
    })

    Vue.component('Msgb', {
        template: '#msgb',
(3.数据接收)
        data(){
            return{
                num:0
            }
        },
(9.定义接收数据)
        methods:{
            output(){
              console.log(this.$attrs.abc)  
            }
        }
    })
    new Vue({
        el: '#app'
    })
Vue 3 中,`<script setup>` 是一种更简洁的语法糖,用于编写组合式 API。它简化了组件的开发流程,但同时也需要我们了解如何在这种语法下实现组件间的数据传输。 以下是如何在 `<script setup>` 模式下实现父子组件间的数据传输。 --- ### 1. 父子组件间的数据传输 #### (1) 父组件向子组件传递数据 父组件可以通过 `props` 向子组件传递数据。 #### 示例代码 ```vue <!-- ParentComponent.vue --> <template> <div> <h1>父组件</h1> <ChildComponent :message="parentMessage" /> </div> </template> <script setup> import ChildComponent from './ChildComponent.vue'; const parentMessage = '这是来自父组件的消息'; </script> ``` ```vue <!-- ChildComponent.vue --> <template> <div> <h2>子组件</h2> <p>{{ message }}</p> </div> </template> <script setup> defineProps(['message']); // 定义接收的 props </script> ``` #### 解释 - 在父组件中,通过 `:message="parentMessage"` 将数据传递给子组件- 在子组件中,使用 `defineProps` 来声明需要接收的属性。 --- #### (2) 子组件向父组件传递数据 子组件可以通过 `emit` 触发事件,将数据传递给父组件。 #### 示例代码 ```vue <!-- ChildComponent.vue --> <template> <div> <h2>子组件</h2> <button @click="sendMessageToParent">发送消息给父组件</button> </div> </template> <script setup> const emit = defineEmits(['child-message']); // 定义触发的事件 function sendMessageToParent() { emit('child-message', '这是来自子组件的消息'); } </script> ``` ```vue <!-- ParentComponent.vue --> <template> <div> <h1>父组件</h1> <p>接收到的消息:{{ childMessage }}</p> <ChildComponent @child-message="handleChildMessage" /> </div> </template> <script setup> import ChildComponent from './ChildComponent.vue'; let childMessage = ''; function handleChildMessage(message) { childMessage = message; } </script> ``` #### 解释 - 在子组件中,使用 `defineEmits` 定义可以触发的事件,并通过 `emit('child-message', data)` 发送数据。 - 在父组件中,通过监听 `@child-message="handleChildMessage"` 接收数据并处理。 --- ### 2. 非父子组件间的数据传输 对于非父子关系的组件(如兄弟组件),可以使用以下方法: #### (1) 使用 Vuex(状态管理工具) Vuex 是 Vue.js 的状态管理库,适用于复杂应用中的全局状态管理。 #### 示例代码 ```javascript // store.js import { createStore } from 'vuex'; export default createStore({ state: { sharedMessage: '这是一个共享的消息', }, mutations: { updateSharedMessage(state, newMessage) { state.sharedMessage = newMessage; }, }, actions: { updateMessage({ commit }, newMessage) { commit('updateSharedMessage', newMessage); }, }, }); ``` ```vue <!-- ComponentA.vue --> <template> <div> <h2>组件 A</h2> <p>共享消息:{{ sharedMessage }}</p> <button @click="updateMessage">更新共享消息</button> </div> </template> <script setup> import { computed } from 'vue'; import { useStore } from 'vuex'; const store = useStore(); const sharedMessage = computed(() => store.state.sharedMessage); function updateMessage() { store.dispatch('updateMessage', '新消息来自组件 A'); } </script> ``` ```vue <!-- ComponentB.vue --> <template> <div> <h2>组件 B</h2> <p>共享消息:{{ sharedMessage }}</p> </div> </template> <script setup> import { computed } from 'vue'; import { useStore } from 'vuex'; const store = useStore(); const sharedMessage = computed(() => store.state.sharedMessage); </script> ``` #### 解释 - `useStore` 是 Vue 3 中用于访问 Vuex Store 的方法。 - 组件 A 和组件 B 通过 `store.state.sharedMessage` 访问共享状态。 - 组件 A 通过 `store.dispatch` 更新状态,组件 B 自动同步更新。 --- #### (2) 使用 Provide/Inject Vue 3 提供了 `provide` 和 `inject`,用于实现跨层级组件通信。 #### 示例代码 ```vue <!-- GrandParentComponent.vue --> <template> <div> <h1>祖父组件</h1> <ParentComponent /> </div> </template> <script setup> import { provide, ref } from 'vue'; import ParentComponent from './ParentComponent.vue'; const sharedMessage = ref('这是一个共享的消息'); provide('sharedMessage', sharedMessage); // 提供数据 </script> ``` ```vue <!-- ChildComponent.vue --> <template> <div> <h2>子组件</h2> <p>共享消息:{{ sharedMessage }}</p> </div> </template> <script setup> import { inject } from 'vue'; const sharedMessage = inject('sharedMessage'); // 注入数据 </script> ``` #### 解释 - 祖父组件通过 `provide` 提供数据。 -组件通过 `inject` 获取数据。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值