Vue中props的使用

本文介绍了Vue.js中Props属性作为父子组件间通信桥梁的作用。详细讲述了如何使用Props将父组件的数据传递给子组件,并展示了在子组件中处理这些数据的方法。此外,还探讨了为Props设置验证规则的重要性。
props属性是父子组件之间的通信桥梁。何为父子组件?从子组件的观点来看,他的上一级实例或组件即为他的父组件。我们知道,处于安全考虑,组件模板里我们无法直接使用父组件的data数据,使用props这个属性可以将父组件的数据传给子组件。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>props的测试</title>
    <script src="../js/vue.js"></script>
</head>
<body>
<div id="props">
    <Child message="父组件的message,我把他的内容重新定义了,但是父组件不会发生改变哦,因为没有绑定,哈哈!!"></Child>
    <hr />
    <input v-model="message"/>
    <Child :message='message'></Child>
</div>
<script>
    Vue.component('Child',{
        props: ['message'],
        template: '<span>{{ message }}</span>'
    });
    var vm = new Vue({
        el: '#props',
        data: {
            message: 'prop的测试'
        }
    });
</script>
</body>
</html>

代码效果图

在子组件中对父组件的数据进行处理。父组件的数据通过props传入子组件以后,在子组件中也可对数据进行相关处理,包括计算属性、data属性等。这样当子组件需要对数据进行处理时,避免了直接在父组件中对数据进行操作,而且由于props数据流单向性,在子组件中更改数据时,不会对父组件的数据产生影响。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>props的测试</title>
    <script src="../js/vue.js"></script>
</head>
<body>
<div id="props">
    <input v-model="message"/>
    <Child :message='message'></Child>
</div>
<script>
    Vue.component('Child',{
        props: ['message','todos'],
        template: '<span>{{ test }}</span>',
        computed: {
            test: function(){
                return this.message.trim().toUpperCase();
            }}
    });
    var vm = new Vue({
        el: '#props',
        data: {
            message: 'prop的测试'
        }
    });
</script>
</body>
</html>

代码效果图

代码效果图

prop的验证
我们可以为组件的 prop 指定验证规则。如果传入的数据不符合要求,Vue 会发出警告。这对于开发给他人使用的组件非常有用。

要指定验证规则,需要用对象的形式来定义 prop,而不能用字符串数组
Vue.component('example',{
    props: {
        propA: String,
        propB: [Number,String]
}
});
Vue 3 中,`props` 是用于父子组件之间传递数据的机制。父组件通过绑定属性的方式向子组件传递数据,子组件通过 `props` 接收这些数据。 --- ### ✅ 示例:Vue 3 中使用 props #### 子组件 `ChildComponent.vue` ```vue <script setup> // 使用 defineProps 定义接收的 props const props = defineProps({ title: { type: String, required: true }, likes: { type: Number, default: 0 }, isPublished: { type: Boolean, default: false } }); </script> <template> <div> <h2>{{ title }}</h2> <p>点赞数:{{ likes }}</p> <p>是否已发布:{{ isPublished ? '是' : '否' }}</p> </div> </template> ``` #### 父组件 `ParentComponent.vue` ```vue <template> <ChildComponent title="Vue3 Props 示例" :likes="100" :is-published="true" /> </template> <script setup> import ChildComponent from './ChildComponent.vue'; </script> ``` --- ### 🔍 解释: - `defineProps()` 是 Vue 3 Composition API 提供的方法,用于声明组件接收的 `props`。 - `title` 是一个必须传入的字符串。 - `likes` 是一个可选的数字,默认值为 `0`。 - `isPublished` 是一个布尔类型,如果未传则默认为 `false`。 - 在模板中使用 `{{ props.xxx }}` 或直接使用 `{{ xxx }}`(因为 `props` 是响应式的且自动解包)。 --- ### 📌 注意事项: 1. **命名规范:** - 父组件中使用的是 kebab-case(短横线命名),如 `is-published` - 子组件中定义的是 camelCase,如 `isPublished` 2. **类型检查:** - 可以使用 `type: [String, Number]` 来支持多个类型 - 如果需要更严格的校验,可以使用自定义验证函数 3. **默认值和必需性:** - `default` 设置默认值 - `required: true` 表示该 prop 必须传入 --- ### ✅ 使用 `<script setup>` 和 `<script>` 的区别 如果你使用的是传统的 `<script>` 而不是 `<script setup>`,写法略有不同: ```js export default { props: { title: { type: String, required: true } } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值