当前位置: 首页 > news >正文

Vue3中props的使用方法以及例子

在 Vue 3 中使用 <script setup> 语法可以让我们更简洁地定义组件内容和逻辑,尤其是在定义 props 时会更方便。以下是使用 <script setup> 语法的所有示例,涵盖 props 的基本用法、类型校验、默认值、只读特性、对象和函数类型的 props、以及解构 props


1. 基本用法

父组件将标题和描述传递给子组件,子组件通过 props 接收并展示这些数据。

<!-- ParentComponent.vue -->
<template><div><ChildComponent title="欢迎" description="这是子组件的描述内容" /></div>
</template><script setup>
import ChildComponent from './ChildComponent.vue';
</script>
<!-- ChildComponent.vue -->
<template><div><h1>{{ title }}</h1><p>{{ description }}</p></div>
</template><script setup>
const props = defineProps({title: String,description: String
});
</script>

2. 类型校验和默认值

在这个示例中,我们为 props 设置了类型和默认值:

<!-- ChildComponent.vue -->
<template><div><h1>{{ title }}</h1><p>{{ description }}</p><p>Views: {{ views }}</p></div>
</template><script setup>
const props = defineProps({title: {type: String,required: true},description: {type: String,default: '这是默认的描述内容'},views: {type: Number,default: 0}
});
</script>

在此示例中:

  • title 为必填项,类型为 String
  • description 有默认值 '这是默认的描述内容'
  • views 的类型为 Number,默认值为 0

3. 基于 props 的计算属性(只读特性)

props 在子组件中是只读的,不能直接修改。以下示例展示了如何基于 props 的值创建计算属性,而不直接修改 props 本身:

<!-- ChildComponent.vue -->
<template><div><h1>{{ fullTitle }}</h1></div>
</template><script setup>
import { computed } from 'vue';const props = defineProps({title: String
});const fullTitle = computed(() => `${props.title} - 子组件`);
</script>

这里使用 computed 创建了一个基于 props.title 的计算属性 fullTitle,不会直接修改 props


4. 传递对象和函数类型的 props

在 Vue 3 中,props 可以传递对象和函数类型的数据,例如用户信息和事件处理函数:

<!-- ParentComponent.vue -->
<template><ChildComponent :user="{ name: '张三' }" :onAction="handleAction" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';function handleAction() {alert('子组件调用的父组件方法');
}
</script>
<!-- ChildComponent.vue -->
<template><div><h2>{{ user.name }}</h2><button @click="onAction">点击处理</button></div>
</template><script setup>
const props = defineProps({user: {type: Object,required: true},onAction: {type: Function,required: true}
});
</script>

在这个例子中,父组件将 user 对象和 onAction 函数作为 props 传递给子组件,子组件可以直接使用这些数据和方法。


5. 使用解构 props

<script setup> 语法中,可以使用解构来简化访问 props。不过需要注意,解构 props 后无法响应更新,因此适用于无需响应更新的情况:

<!-- ChildComponent.vue -->
<template><div><h1>{{ title }}</h1><p>{{ description }}</p></div>
</template><script setup>
const { title, description } = defineProps({title: String,description: String
});
</script>

在这个例子中,titledescription 通过解构 props 直接使用,代码更加简洁,但不具备响应式。


6. 监听 props 变化

可以使用 watch 监听 props 的变化,从而响应父组件传递的数据更新:

<!-- ChildComponent.vue -->
<template><div><h1>{{ title }}</h1></div>
</template><script setup>
import { watch } from 'vue';const props = defineProps({title: String
});watch(() => props.title,(newVal, oldVal) => {console.log(`title changed from ${oldVal} to ${newVal}`);}
);
</script>

props.title 发生变化时,watch 函数会记录并输出新旧值。


http://www.mrgr.cn/news/60824.html

相关文章:

  • SCI一区级 | Matlab实现SSA-TCN-LSTM-Attention多变量时间序列预测
  • hi3536上ffmpeg带rtmp移植
  • 国产电脑能装win系统吗_国产电脑安装windows要求及方法
  • netty之导入源码到idea
  • 查找接口成功率最优时间段
  • MySQL(2)【库的操作】
  • OpenCV图像处理方法:腐蚀操作
  • flutter实战短视频课程
  • docker 相关操作命令
  • 前端项目代码风格及校验统一格式化配置
  • 代码随想录算法训练营第十三天|二叉树的递归遍历、 二叉树的迭代遍历、二叉树的层次遍历
  • 常见学习陷阱及解决方案
  • 认识线程 — JavaEE
  • 论文精读:Approximating Maximin Share Allocations(上)
  • java中的二叉树
  • MinIO服务部署指南
  • < 背包问题 >
  • 多源BFS问题(1)_01矩阵
  • Tangible Software Solutions 出品最准确可靠的源代码转换器
  • 大数据新视界 -- 大数据大厂之大数据重塑影视娱乐产业的未来(4 - 2)
  • DispatchingController
  • Java Lock ConditionObject 总结
  • 优先算法——复写零(双指针)
  • BFS解决最短路问题(4)_为高尔夫比赛砍树
  • 北理工软件工程考研难度分析
  • 解决VMware虚拟机的字体过小问题