Vue的props里default: ()=> {}写法一个小坑
2023-08-06 本文已影响0人
FrankFang7
下面这种常见的这种写法是有问题的
props: {
myArray: {
type: Array,
default: ()=> []
},
myObject: {
type: Object,
default: ()=> {}
},
},
created() {
cosole.log("this.myObject",this.myObject) // this.myObject undefined
},
最简单的调整是加个括号
props: {
myArray: {
type: Array,
default: ()=> []
},
myObject: {
type: Object,
default: ()=> ({})
},
}