项目场景:
Props with type Object/Array must use a factory function to return the default value.
问题描述
解决vue props传Array/Object类型值,子组件报错的情况
props:{
projectTypes: {
type: Array,
default: []
}
}
或者
props:{
projectTypes: {
type: Object,
default: {}
}
}
原因分析:
props default 数组/对象的默认值应当由一个工厂函数返回
解决方案:
// 数组
// 方案1
props: {
projectTypes: {
type: Array,
default:()=>[]
}
}
// 方案2
props: {
projectTypes: {
type: Array,
default:function(){
return [];
}
}
},
// 对象
// 方案1
props: {
projectTypes: {
type: Object,
default:()=> {}
}
}
// 方案2
props: {
projectTypes: {
type: Object,
default() {
return {};
}
}
},