Vue3组件传值

文章详细介绍了在Vue3中如何进行组件间的通信,包括父组件向子组件传递数据使用`defineProps`,子组件向父组件传递数据利用`defineEmits`触发自定义事件,以及如何借助mitt插件实现兄弟组件之间的通信。

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

1、父传子

父组件中:

<template>
  <div class="box">父组件</div>
  <Child title="传给子组件" :msg="msg"></Child>
</template>

<script setup lang="ts">
import Child from "./Child.vue";
const msg = "一条消息";
</script>

<style scoped>
.box {
  width: 500px;
  height: 300px;
  background: #dcd8d8;
}
</style>

使用setup组合式api就不能使用export default{}

子组件中使用defineProps接收数据,defineProps是vue3提供的方法,接收到的数据是只读的

<template>
  <div class="son">子组件--{{ title }}--{{ msg }}</div>
</template>

<script setup lang="ts">
// 需要使用defineProps方法去接受父组件传递过来的数据
// defineProps是vue3提供的方法
// props的数据是只读的
let props = defineProps(["title", "msg"]);
console.log(props);
</script>

<style scoped>
.son {
  width: 300px;
  height: 100px;
  background: pink;
}
</style>

2、子传父

在子组件中利用defineEmits方法里返回函数触发自定义事件

<template>
  <div class="son">子组件</div>
  <button @click="handel">点击触发自定义事件</button>
</template>

<script setup lang="ts">
// 利用defineEmits方法里返回函数触发自定义事件
let emit = defineEmits([]);
const handel = () => {
  // 第一个参数是事件类型,之后的参数就是要传递的数据
  emit("change", "改变后的值");
};
</script>

父组件中通过子组件的自定义方法接收数据

<template>
  <event @change="getMsg"></event>
</template>

<script setup lang="ts">
import event from "./Event1.vue";
const getMsg = (data) => {
  console.log(data);
};
</script>

3、兄弟组件传值

引入mitt插件

 

 

### Vue3 组件与监听 #### 使用 `props` 和 `$emit` 父组件可以通过定义属性来向子组件递数据。这些属性被称为 `props`。当需要从子组件触发事件通知父组件时,则可以使用 `$emit` 方法。 ```html <!-- ParentComponent.vue --> <template> <div> <!-- 向 ChildComponent 递 message 属性 --> <ChildComponent :message="parentMessage" @childEvent="handleChildEvent"/> </div> </template> <script setup> import { ref } from &#39;vue&#39;; const parentMessage = ref(&#39;Hello, child!&#39;); function handleChildEvent(data){ console.log(`Received data from child component: ${data}`); } </script> ``` ```html <!-- ChildComponent.vue --> <template> <button @click="sendMessageToParent">Send Message to Parent</button> </template> <script setup> defineProps([&#39;message&#39;]); function sendMessageToParent(){ defineEmits()(&#39;childEvent&#39;, &#39;Data sent by the child&#39;); } </script> ``` 此方法适用于父子关系中的单向数据流[^1]。 #### 使用 `v-model` 对于双向绑定场景,比如表单项,默认情况下可以用 `v-model` 来简化输入框和其他控件的数据同步操作。 ```html <!-- ParentComponent.vue --> <template> <input v-model="searchText"> <CustomInput v-model="searchText"></CustomInput> </template> <script setup> import CustomInput from &#39;./components/CustomInput.vue&#39; import {ref} from "vue"; let searchText = ref(&#39;&#39;); </script> ``` ```html <!-- CustomInput.vue --> <template> <input type="text" :value="modelValue" @input="$emit(&#39;update:modelValue&#39;, $event.target.value)"> </template> <script setup> defineProps({ modelValue: String, }); </script> ``` 这种方式使得自定义组件能够像内置元素一样支持 `v-model`[^2]。 #### 使用 `provide` / `inject` 如果多个嵌套层次的组件都需要访问相同的状态或函数,那么通过 `provide` 提供者模式可以在祖先组件中声明资源,在后代组件里注入并消费它们。 ```javascript // App.vue (Ancestor Component) export default { provide () { return { sharedState: this.sharedState, updateSharedState: this.updateSharedState } }, methods:{ updateSharedState(newValue){ this.sharedState=newValue; } } }; ``` ```javascript // AnyDescendant.vue (Any Descendant Component) export default { inject: [&#39;sharedState&#39;,&#39;updateSharedState&#39;], mounted(){ console.log(this.sharedState); } }; ``` 这允许跨多层组件共享状态而无需逐级递参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值