3分钟了解 vue props type类型

本文详细探讨了Vue中prop的类型设定,包括String、Number、Boolean、Array、Object等,并讲解了如何使用propType进行类型检查和设置默认值、验证函数。带你掌握Vue组件间数据传递的严谨方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用了很久的vue,有时候总觉得props type类型,总是有点模棱两可,今天来好好的盘盘他

 props介绍:

都知道props是用来父给子传值的(单向的),HTML 中的 attribute 名是大小写不敏感的,这意味着当你使用 DOM 中的模板时,camelCase (驼峰命名法) 的 prop 名需要使用其等价的 kebab-case (短横线分隔命名) 命名

 props类型:

props type使用的目的,有点像typescript那种类型检查, type的类型有如下几种,

  • String
  • Number
  • Boolean
  • Array
  • Object
  • Date
  • Function
  • Symbol

props的写法:

第一种简单的写法(无默认值):

props: {
  title: String,
  likes: Number,
  isPublished: Boolean,
  commentIds: Array,
  author: Object,
  callback: Function,
  contactsPromise: Promise 
}

第二种有默认值的写法:

注意:type 是 对象类型的,default 要使用工厂函数获取,例子如下

props: {
    // 基础的类型检查 (`null` 和 `undefined` 会通过任何类型验证)
    propA: Number,
    // 多个可能的类型
    propB: [String, Number],
    // 必填的字符串
    propC: {
      type: String,
      required: true
    },
    // 带有默认值的数字
    propD: {
      type: Number,
      default: 100
    },
    // 带有默认值的对象
    propE: {
      type: Object,
      // 对象或数组默认值必须从一个工厂函数获取
      default: function () {
        return { message: 'hello' }
      }
    },
    // 自定义验证函数
    propF: {
      validator: function (value) {
        // 这个值必须匹配下列字符串中的一个
        return ['success', 'warning', 'danger'].indexOf(value) !== -1
      }
    }
  }

### Vue Props 数据类型的定义与使用 在 Vue.js 中,`props` 是用于接收来自父组件的数据。为了确保这些数据的正确性和可靠性,可以为 `props` 指定类型和其他验证规则。 #### 基本语法 当声明一个 prop 时,可以通过对象的形式来指定其类型以及默认值: ```javascript Vue.component(&#39;example-component&#39;, { props: { message: { type: String, default: &#39;Default message&#39; } }, template: &#39;<span>{{ message }}</span>&#39; }); ``` 这段代码展示了如何设置字符串类型的 `message` 属性,并为其指定了默认消息[^2]。 #### 支持的数据类型 除了基本的 JavaScript 类型外,还可以接受数组和对象作为参数。以下是几种常见的类型及其对应的构造函数: - **String**: 文本串。 - **Number**: 数字。 - **Boolean**: 布尔值。 - **Array**: 数组。 - **Object**: 对象。 - **Date**: 日期实例。 - **Function**: 函数。 - **Symbol**: 符号。 对于复杂结构如自定义类或枚举,则可以直接传入相应的构造器: ```javascript class CustomClass {} const enumType = Object.freeze({ ONE: 1, TWO: 2 }); Vue.component(&#39;custom-component&#39;, { props: { customInstance: CustomClass, selectedEnumValue: Number // Assuming the values of enum are numbers. } }); ``` 此外,在 DOM 模板中使用驼峰命名法 (camelCase) 的属性名应转换成短横线分割形式 (kebab-case),例如 `postTitle` 应该写作 `post-title`[^4]: ```html <blog-post post-title="hello!"></blog-post> ``` #### 复杂示例 下面的例子展示了一个更复杂的场景,其中包含了多种不同类型props 和它们各自的验证条件: ```javascript Vue.component(&#39;complex-component&#39;, { props: { title: { type: String, required: true }, likes: { type: Number, validator(value) { return value >= 0; } }, isPublished: Boolean, commentIds: Array, author: { type: Object, default() { return {}; } }, callback: Function, contactsPromise: Promise // any constructor function will do here } }); ``` 在这个例子中,不仅限定了每种 prop 的具体类型,还加入了额外的约束条件,比如 `likes` 只能是非负数,而 `title` 被标记为必填项[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

时光机上敲代码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值