繁体   English   中英

Vue JS中的道具数组?

[英]Array of props in vue js?

在Javascript中,我可以使用异构数组,例如:

var ex = ['name', 12, true];
console.log(ex);

在单个文件模板中的Vue JS中,我可以通过以下方法在<script></script部分中为组件定义道具:

export default{
props: ['myprop']
}

或通过将道具列为对象来验证类型

export default{
  props: {
    myprop: String
  }
}

现在,我的问题是在myprop: [String,Array]列出类似myprop: [String,Array]的类型数组myprop: [String,Array]列出该属性的多个有效类型。

我的问题是我如何才能在道具级别针对数组的内容进行验证?

例如,采用字符串,数字,布尔值和3的计数形式的ex。如果不是这种形式,有没有办法使prop中包含的任何值无效?

因此,如果我以[true, 12, 'name']的形式获取了一些数据,那将是无效的。 但是前者是有效的。

如果遵循,您想使用自定义属性验证器函数。 就像是:

props: {
  myprop: {
    type: Array,
    validator: value => {
       return /* test value length, indices types, etc here as truthy/falsy */;
    }
  }
}

type属性只是针对类型的简单验证,自定义验证器允许您精确地自定义构成组件的有效属性的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM