文章目录
- 一、Props
- 二、v-model
- 三、Provide/Inject:
- 四、事件
- 四、Ref
在 Vue 3 中,父子组件之间进行通信有多种方式,下面简单介绍下常见的方式及其用法和使用场景:
一、Props
用于父组件向子组件传递数据。
这是最基本也是最常用的一种方式。通过在子组件上定义 props,父组件可以将数据传递给子组件。在子组件中,通过 props 对象访问这些属性。
父组件:
<template><ChildComponent :message="parentMessage" />
</template><script>
import ChildComponent from './ChildComponent.vue';export default {components: {ChildComponent,},data() {return {parentMessage: 'Hello from parent!',};},
};
</script>
子组件:
<template><div>{{ message }}</div>
</template><script>
export default {props: {message: String,},
};
</script>
二、v-model
用于在父子组件之间实现双向绑定。在 Vue 3 中,通过 v-model 方式进行组件通信需要使用 v-model 指令和 emit 事件。父组件使用 v-model 向子组件传递数据,并通过子组件触发 ,用update:modelValue 事件来实现双向绑定。
下面是一个简单的例子:
子组件:
<!-- ChildComponent.vue -->
<template><input :value="message" @input="$emit('update:modelValue', $event)" />
</template>
<script>
export default {props: {modelValue: String,},computed: {message: {get() {return this.modelValue;},set(value) {this.$emit('update:modelValue', value);},},},
};
</script>
父组件:
<!-- ParentComponent.vue -->
<template><ChildComponent v-model="parentMessage" />
</template><script>
import ChildComponent from './ChildComponent.vue';export default {components: {ChildComponent,},data() {return {parentMessage: 'Hello from parent!',};},
};
</script>
在子组件 ChildComponent 中,通过 :value=“modelValue” 将 modelValue 绑定到 input 元素上,然后通过 @input=“$emit(‘update:modelValue’, $event)” 触发 update:modelValue 事件,从而实现了父子组件之间的双向绑定。
在父组件 ParentComponent 中,使用 v-model 将 ChildComponent 的 modelValue 绑定到 message 上,这样在父组件中修改 message 的值会自动同步到 ChildComponent 中,反之亦然。
需要注意的是,v-model 实际上是一个语法糖,它会自动处理 value 和 input 事件。如果在子组件中使用 v-model,则子组件应该接受名为 modelValue 的 prop,并发出一个名为 update:modelValue 的事件。这样可以确保 v-model 在父子组件之间正确地进行双向绑定。
三、Provide/Inject:
用于祖先组件向后代组件传递数据,通过 Provide 提供数据,通过 Inject 注入数据。祖先组件通过 provide 提供数据,后代组件通过 inject 接收数据。
祖先组件:
<template><GrandparentComponent><template v-slot="{ message }"><ChildComponent :message="message" /></template></GrandparentComponent>
</template>
<script>
import GrandparentComponent from './GrandparentComponent.vue';export default {components: {GrandparentComponent,},provide() {return {message: 'Hello from grandparent!',};},
};
</script>
父组件:
<template><slot :message="message" />
</template><script>
export default {inject: ['message'],
};
</script>
子组件:
<template><div>{{ message }}</div>
</template><script>
export default {props: {message: String,},
};
</script>
四、事件
通过自定义事件,子组件向父组件传递数据。子组件通过 $emit 触发自定义事件,父组件监听该事件接收数据。
子组件:
<template><button @click="sendMessage">Send Message</button>
</template><script>
export default {methods: {sendMessage() {this.$emit('message', 'Hello from child!');},},
};
</script>
父组件:
<template><ChildComponent @message="handleMessage" />
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {components: {ChildComponent,},methods: {handleMessage(message) {console.log(message);},},
};
</script>
四、Ref
使用 ref 可以将数据在父子组件之间共享。
父组件:
<template><ChildComponent :message="sharedMessage" />
</template>
<script>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';
export default {components: {ChildComponent,},setup() {const sharedMessage = ref('Hello from parent!');return { sharedMessage };},
};
</script>
子组件:
<template><div>{{ message }}</div>
</template>
<script>
import { ref, watchEffect } from 'vue';export default {props: {message: String,},setup(props) {const message = ref(props.message);watchEffect(() => {// 监听 props 中的 message 变化message.value = props.message;});return { message };},
};
</script>