Vue3 中的 defineProps:优雅地实现子父组件传值

Vue.js 一直以来都是前端开发者们钟爱的框架之一,而在最新的 Vue 3 中,引入了一些新的特性和改进,其中之一就是 definePropsdefineProps 提供了一种更加明确和类型安全的方式来定义子组件的 props,让子父组件之间的数据传递更加清晰和可维护。

本文将介绍 Vue 3 中 defineProps 的用法以及如何在子组件中使用它来接收父组件传递的 props 数据。

什么是 defineProps?

defineProps 是 Vue 3 中新引入的函数,用于定义子组件的 props。

与 Vue 2 中的 props 不同,defineProps 更加明确和类型安全,它使用 TypeScript 或者 PropTypes 来明确指定 props 的类型和默认值,从而提供更好的开发体验和代码健壮性。

如何使用 defineProps?

在 Vue 3 中,使用 defineProps 定义子组件的 props 非常简单。下面是一个简单的例子:

import { defineComponent, defineProps } from 'vue';

const MyComponent = defineComponent({
  setup() {
    const props = defineProps<{
      message: string;
      count: number;
    }>();

    return { props };
  },
  template: `<div>{{ props.message }} - {{ props.count }}</div>`
});

在上面的例子中,我们首先使用 defineProps 定义了 messagecount 两个 props,并指定了它们的类型分别为 stringnumber

然后在组件的 setup 函数中通过 defineProps 来获取 props 对象,并将其返回,以便在模板中使用。

使用 defineProps 接收父组件传递的 props

在父组件中,我们可以像以前一样通过属性的形式传递数据给子组件。而在子组件中,则可以通过 defineProps 来接收这些 props 数据。

下面是一个父子组件之间传值的例子:

父组件

// ParentComponent.vue
<template>
  <div>
    <MyComponent :message="parentMessage" :count="parentCount" />
  </div>
</template>

<script>
import MyComponent from './MyComponent.vue';

export default {
  components: {
    MyComponent,
  },
  data() {
    return {
      parentMessage: 'Hello Java轮子',
      parentCount: 10,
    };
  },
};
</script>

子组件 

// MyComponent.vue
<template>
  <div>{{ props.message }} - {{ props.count }}</div>
</template>

<script>
import { defineComponent, defineProps } from 'vue';

export default defineComponent({
  setup() {
    const props = defineProps<{
      message: string;
      count: number;
    }>();

    return { props };
  },
});
</script>

在上面的例子中,父组件 ParentComponent 通过 parentMessageparentCount 两个属性向子组件 MyComponent 传递了数据。

而在子组件中,则通过 defineProps 来接收这些数据,并在模板中进行展示。

总结

defineProps 是 Vue 3 中一个非常强大的特性,它提供了一种更加明确和类型安全的方式来定义子组件的 props。通过使用 defineProps,我们可以让子父组件之间的数据传递更加清晰和可维护,同时也提高了代码的健壮性。

希望本文能够帮助你更好地理解和使用 Vue 3 中的 defineProps

不管做什么,只要坚持下去就会不一样!

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3,父组件向子组件传值有多种方法。其一种方法是使用`defineProps`,在父组件定义props属性,然后将需要传递给子组件的值作为props的属性值。在子组件,可以使用`props`接收父组件传递的值。 具体实现步骤如下: 1. 在父组件使用`defineProps`定义props属性,并将需要传递给子组件的值作为props的属性值。 2. 在子组件使用`props`接收父组件传递的值。 下面是一个示例代码,展示了如何在Vue3实现父向子组件传值: ```javascript // 父组件 Father.vue <template> <div class="fa"> <div style="margin: 10px;">我是父组件</div> 父组件接收子组件传的值:{{ sonMessage }} <Son :message="sonMessage"></Son> </div> </template> <script setup lang="ts"> import Son from './Son.vue' import { ref } from "vue"; const sonMessage = ref<string>(""); </script> <style scoped> .fa { border: 3px solid cornflowerblue; width: 400px; text-align: center; } </style> ``` ```javascript // 子组件 Son.vue <template> <div class="son"> <div style="margin: 10px;">我是子组件</div> 子组件接收父组件传的值:{{ message }} </div> </template> <script setup lang="ts"> import { defineProps } from "vue"; const props = defineProps({ message: String, // 定义props属性,接收父组件传递的值 }); </script> <style scoped> .son { border: 3px solid lightgreen; width: 200px; text-align: center; } </style> ``` 在父组件,使用`:message="sonMessage"`将`sonMessage`的值传递给子组件的`message` props属性。在子组件,使用`props`接收父组件传递的值,并在模板展示。 这样,就实现了在Vue3父组件向子组件传递值的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Vue3父子组件间传参通信](https://blog.csdn.net/qq_45397526/article/details/126281133)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue3 父组件和子组件如何传值 详解](https://blog.csdn.net/qq_56263094/article/details/124576055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

良月柒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值