Vue props中Object和Array设置默认值
下面这三种方式都可以:
js
seller: {
type: Object,
default() {
return {}
}
}
seller: {
type: Object,
default: function () {
return {}
}
}
seller: {
type: Object,
default: () => ({})
}
下面这种是错误的:
js
seller: {
type: Object,
default: () => {}
}
原因:当父组件没有传这个值或者值是空时,输出的话,这时是返回underfind,在template中获取里面的值时,就报错