【发布时间】:2019-01-27 10:29:39
【问题描述】:
我有定义类型的道具,但我希望它们中的最后一个接受任何类型的值
props: {
Size: String,
Label: String,
Name: String,
value: Any
}
我能做到这一点吗?
【问题讨论】:
-
将其设置为
null以匹配任何类型。
标签: vue.js
我有定义类型的道具,但我希望它们中的最后一个接受任何类型的值
props: {
Size: String,
Label: String,
Name: String,
value: Any
}
我能做到这一点吗?
【问题讨论】:
null 以匹配任何类型。
标签: vue.js
【讨论】:
null 和 undefined 将通过任何类型验证 - 只需确保 required 未设置为 true。 Found it here.
当你有定义类型的道具时支持道具类型any:
props: {
custom1: null, // set as `null`
custom2: undefined, // or `undefined`
// if you need to add extra rules, simply don't set the `type` prop
custom3: {
required: true,
//... rest of your props
}
}
【讨论】:
这是我为通过 lint 验证所做的工作。
您可以在这里进行真正的验证。
lodash 提供了许多方便的方法来检查类型
value: {
required: true,
validator: v => true
},
【讨论】:
validator: v => !_.isUndefined(v)