如果将props定义为数组,有如下几个缺点:
1.无法进行数据类型的限制
2.无法对数据进行自定义的校验
解决:将props定义为对象
对象类型:通过对象的方式定义props成员,可以为每个prop成员制定规则(类型,校验...),常用的如下 :
一,单一类型限制:
通过type属性限制类型
类型要指定为构造函数类型,不要加引号,常见类型有:
String | Number | Boolean | Array | Object | Function
props: {
id: {
type: String,
required: true
},
isEdit: {
type: Boolean,
required: true
},
originList: {
type: Array,
required: true
}
}
.....
二,必填项校验:
通过required属性进行限制,如果后期没有传递这个数据,则报错:missing required prop
(代码可看上面)
三,属性默认值:
通过default属性设置默认值
为props成员设置默认值,如果没有传递数据,则使用默认值&#