Vue 2-如何在props中设置数组的默认类型 [英] Vue 2 - How to set default type of array in props
本文介绍了Vue 2-如何在props中设置数组的默认类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的Vue组件,该组件将一组对象作为道具.我经常使用道具验证,尤其是对于默认"值功能.
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.
在这种情况下,我有:
props: {
items: Array
}
但我希望它在Typescript或React中具有以下效果:
but I'd like it to have like in Typescript or React:
props: {
items: Array.of(
{key: {type: String, default: 'myText'}}
)
}
等
有可能实现吗?否则,我只需要使用计算数据作为映射即可设置默认值
Is it possible to achieve? Otherwise I need to use computed data as map just to set the defaults
推荐答案
我创建了示例: jsFiddle ,这可能会对您有所帮助,是的...您可以将默认值作为数组返回:
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: () => []
}
}
这篇关于Vue 2-如何在props中设置数组的默认类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文