Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Vue 3 子组件接收父组件传参数

Vue 3 子组件接收父组件传参数

作者头像
訾博ZiBo
发布于 2025-01-06 11:23:30
发布于 2025-01-06 11:23:30
5700
代码可运行
举报
运行总次数:0
代码可运行

Vue 3 子组件接收父组件传参数

一、子组件接收参数

1、子组件

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<script setup lang="ts">
defineProps<{
  msg: string,
  student: {
    name: string,
    age: number,
  },
  students: {
    name: string,
    age: number,
  }[]
}>()
</script>

<template>
  <h1>简单传值:{{ msg }}</h1>
  <h1>传对象:{{ student }}</h1>
  <h1>传数组第一个值:{{ students[0] }}</h1>
  <h1>传数组第二个值:{{ students[1] }}</h1>
</template>

<style scoped>
</style>
带默认值写法
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// 接收父组件传参,带默认值
const props = withDefaults(defineProps<{
  row?: DocSend,
  isEdit?: boolean
}>(), {
  row: undefined,
  isEdit: false
});

2、父组件

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<template>
  <Hello msg="訾博" :student="student" :students="students" />
</template>

<script setup lang="ts">
import Hello from "./components/Hello.vue";
// 对象
const student = {
  name: "訾博",
  age: 26,
};
// 对象数组
const students = [
  {
    name: "訾博1",
    age: 26,
  },
  {
    name: "訾博2",
    age: 26,
  },
];
</script>

3、运行结果

二、相关探索结果

1、父组件中的响应式属性的值被修改后,该属性在子组件中的值也会被修改; 2、子组件中使用 const props = defineProps<{ msg: string }>() 定义接收的属性,可以直接访问,也可以通过 props 访问; 3、子组件中使用 const props = defineProps<{ msg: string }>() 定义接收的属性,子组件中的 props.msg 的值不是响应式的,是只读的,无法修改;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验