问题
I have my Vue component, which is taking an array of objects as a prop. I often use prop validation, especially for 'default' value feature.
in this case I have:
props: {
items: Array
}
but I'd like it to have like in Typescript or React:
props: {
items: Array.of(
{key: {type: String, default: 'myText'}}
)
}
etc.
Is it possible to achieve? Otherwise I need to use computed data as map just to set the defaults
回答1:
I created example: jsFiddle, that might can help you, and yes... you can return the default value as a array:
ES6
props: {
items: {
type: Array,
default: () => []
}
}
回答2:
ES6 variety For an Array
props: {
arr: {
type: Array,
default: () => []
}
}
...And for an Object
props: {
obj: {
type: Object,
default: () => ({
param: value,
param2: value,
})
}
}
A couple related resources:
- https://github.com/vue-styleguidist/vue-styleguidist
- https://github.com/vuejs/vue/issues/1032
来源:https://stackoverflow.com/questions/41553596/vue-2-how-to-set-default-type-of-array-in-props