目录一、简单数据类型1、布尔类型 Boolean正确写法 :2、数字类型 Number正确写法 :3、字符串类型 String正确写法 :二、复杂数据类型1、数组 Array错误写法
这篇文章主要介绍了Vue子组件内的props对象里的default参数是如何定义
Array、Object、或Function默认值的正确写法说明,具有很好的参考价值
props: {
demoBoo: {
type: Boolean,
default: true,
},
},
props: {
demoNum: {
type: Number,
default: 1,
},
},
props: {
demoStr: {
type: String,
default: 'hello',
},
},
props: {
demoArr: {
typeof: Array,
default: [],
},
},
Invalid default value for prop “demo”: Props with type Object/Array must use a factory function to return the default value.
props: {
demoArr: {
type: Array,
default: function () {
return [];
},
},
},
props: {
demoArr: {
type: Array,
default: () => [],
},
},
props: {
demoObj: {
type: Object,
default: () => {},
},
},
props: {
demoObj: {
type: Object,
default: function () {
return {};
},
},
},
或是用 箭头函数 :( 注意 : 这里的对象一定要用小括号包裹起来( { } ))
props: {
demoObj: {
type: Object,
default: () => ({}),
},
},
props: {
demoFun: {
type: Function,
default: () => {},
},
},
补充知识 :Vue 传参 props 里面为什么要带 type , 还有 default ?
这个是子组件, 写 type 的 意思 是 swiperDate 传过来的数据类型是 数组 ,
default就是 表示 不传 ,默认返回 的 [ ] , 空数组 .
这种就是 表示 传的数据类型是Number, 不传默认是 数字 0 。
到此这篇关于Vue子组件内的props对象参数配置的文章就介绍到这了,更多相关Vue子组件对象参数配置内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Vue子组件内的props对象参数配置方法
本文链接: https://www.lsjlt.com/news/166055.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0