vue3 父传子

Vue父子页面传值与接收

父页面传值

<template>
  <div>
    <child-component :parentData="message" />
  </div>
</template>

<script setup>
import ChildComponent from './ChildComponent.vue';
const message = ref('Hello from parent!');
</script>

子页面接收

<template>
  <div>
    {{ parentData }}
  </div>
</template>
 
<script setup>
import { defineProps } from 'vue';
 
const props = defineProps({
  parentData: String
});
</script>
Vue 3中,组件向子组件传值主要通过属性绑定的方式实现,以下为详细介绍: ### 基本属性绑定传值 在组件中,使用属性绑定向子组件传递数据。例如,组件代码如下: ```vue <template> <div class="parent"> <h1>我是组件</h1> <!-- 使用属性绑定传递数据 --> <ChildVue :titles="title" :contents="content" /> </div> </template> <script setup> import { ref } from &#39;vue&#39;; import ChildVue from &#39;./ChildVue.vue&#39;; // 定义组件的数据 const title = ref(&#39;组件的标题&#39;); const content = ref(&#39;组件传过来的内容&#39;); </script> <style scoped> .parent { width: 500px; height: 500px; background: rgb(146, 133, 133); } </style> ``` 在上述代码中,`:titles="title"` 和 `:contents="content"` 是将组件的 `title` 和 `content` 属性值传递到子组件所绑定的 `titles` 和 `contents` 属性中。 子组件接收数据,代码如下: ```vue <template> <div class="child"> <h2>{{ titles }}</h2> <p>{{ contents }}</p> </div> </template> <script setup> import { defineProps } from &#39;vue&#39;; // 定义接收的属性 const props = defineProps({ titles: String, contents: String }); </script> <style scoped> .child { width: 300px; height: 300px; background: rgb(200, 200, 200); } </style> ``` 这里使用 `defineProps` 来定义子组件接收的属性,子组件就可以使用这些属性的值了。 ### 动态传值与默认值 可以给 `defineProps` 中的属性设置默认值。例如,子组件代码如下: ```vue <template> <div class="child"> <h2>{{ titles }}</h2> <p>{{ contents }}</p> </div> </template> <script setup> import { defineProps } from &#39;vue&#39;; // 定义接收的属性,并设置默认值 const props = defineProps({ titles: { type: String, default: &#39;默认标题&#39; }, contents: { type: String, default: &#39;默认内容&#39; } }); </script> <style scoped> .child { width: 300px; height: 300px; background: rgb(200, 200, 200); } </style> ``` 当组件没有传递相应的值时,子组件会使用默认值。 ### 通过 `ref` 调用子组件方法并传值 在组件中,可以通过 `ref` 调用子组件的方法并动态传参。例如: ```vue <template> <div class="parent"> <h1>我是组件</h1> <!-- 给子组件定义 ref --> <ChildVue ref="child" /> <button @click="invokeChildMethod">调用子组件方法</button> </div> </template> <script setup> import { ref } from &#39;vue&#39;; import ChildVue from &#39;./ChildVue.vue&#39;; // 定义子组件的 ref const child = ref(null); // 调用子组件方法,动态传参 const invokeChildMethod = () => { child.value?.printAge(12); }; </script> <style scoped> .parent { width: 500px; height: 500px; background: rgb(146, 133, 133); } </style> ``` 子组件代码如下: ```vue <template> <div class="child"> <h2>我是子组件</h2> </div> </template> <script setup> import { defineExpose } from &#39;vue&#39;; // 定义子组件方法 const printAge = (age) => { console.log(`年龄是:${age}`); }; // 暴露方法给组件调用 defineExpose({ printAge }); </script> <style scoped> .child { width: 300px; height: 300px; background: rgb(200, 200, 200); } </style> ``` 在这个例子中,组件通过 `ref` 调用子组件的 `printAge` 方法,并传递了参数 `12`。 ### 总结 Vue 3组件向子组件传值主要通过属性绑定和 `ref` 调用子组件方法的方式实现。属性绑定可以传递数据,而 `ref` 可以调用子组件的方法并传参。同时,子组件可以使用 `defineProps` 来接收和处理组件传递的数据,还可以设置默认值。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值