admin管理员组文章数量:1323330
Is there any way to track nested property types on Vue?
I have this props setup:
// ...
props: {
navigation: {
type: [Array],
default: () => {
return [
{ type: String, icon: "home" },
{ type: String, icon: "star" }
]
}
}
}
right now i have validation only for first level which is array, but nothing happens inside of it. Can i check types inside one of type level? Thx for help.
Is there any way to track nested property types on Vue?
I have this props setup:
// ...
props: {
navigation: {
type: [Array],
default: () => {
return [
{ type: String, icon: "home" },
{ type: String, icon: "star" }
]
}
}
}
right now i have validation only for first level which is array, but nothing happens inside of it. Can i check types inside one of type level? Thx for help.
Share edited May 11, 2017 at 11:59 Lukas asked May 11, 2017 at 11:03 LukasLukas 7,74420 gold badges79 silver badges127 bronze badges 7- What do you mean by "track nested property types"? Do you mean, be able to assign types to them and have the piler warn you when you are not plying with those types? – AJP Commented May 11, 2017 at 11:14
- yeap, that's right – Lukas Commented May 11, 2017 at 11:15
-
And are you referring to the return type from the
default
function? – AJP Commented May 11, 2017 at 11:15 - yes, but what with the next level, i.e. of this icon strings? – Lukas Commented May 11, 2017 at 11:16
-
What do you want
navigation
to be? You have currently said it's type should be an Array but the default function you have defined returns an array of objects (with keys type and icon). Note from the examples in the docs you posted it doesn't look like you type the return value from the default function. – AJP Commented May 11, 2017 at 12:04
3 Answers
Reset to default 4I don't think it's possible to directly type the array of objects using the type
attribute as Vue prop validation says:
The
type
can be one of the following native constructors:
- String
- Number
- Boolean
- Function
- Object
- Array
- Symbol
Having said that it looks like you could define a validator function like:
props: {
navigation: {
validator: function (value) {
if (!(value instanceof Array)) {
return false;
}
for(var i = 0; i < value.length; ++i) {
if (typeof value.icon !== "string") {
return false;
}
}
return true;
},
default: () => {
return [
{icon: "home" },
{icon: "star" }
]
}
}
}
In addition to typescript and library like vue-types, I find _.every
and _.has
from lodash can make pretty readable solution for validating plicated prop.
import every from 'lodash/every'
import has from 'lodash/has'
export default {
name: 'navigation',
props: {
items: {
type: Array,
validator: items =>
every(items, (item) =>
has(item, 'home') &&
has(item, 'star')
)
}
}
}
You can even write has(item, 'home.adress.zip')
for deep nest object!
vue-types makes this easy to do. In the following example, notice the Array of strings:
props: {
picNum: VueTypes.number.def( 3 ),
pics: VueTypes.arrayOf( String ).isRequired, // RIGHT HERE!
step: VueTypes.number.def( 1 ),
className: VueTypes.string.def( '' ),
},
Plus vue-types has more helpers for defining the shape of objects, etc. Super nice to use with Vue props!
本文标签: javascriptNested property types in VuejsStack Overflow
版权声明:本文标题:javascript - Nested property types in Vue.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742139383a2422513.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论