简体   繁体   中英

Array of props in vue js?

In Javascript I can have heterogenous arrays such as:

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

In Vue JS within a single file template I can define props for a component in the <script></script section by the following:

export default{
props: ['myprop']
}

or by having the props listed as an object to validate the type

export default{
  props: {
    myprop: String
  }
}

Now my question is in vue listing an array of types like myprop: [String,Array] lists multiple valid types for the property.

My question is how can I validate against the content of the array at the props level?

For instance taking ex in the pattern of string,number,boolean and a count of 3. Is there a way to make any value that comes into the prop be invalid if it's not this form?

So if I got some data in the form of [true, 12, 'name'] it'd be invalid. But ex would be valid.

If I follow, you want to use a custom property validator function. Something like:

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

The type property is just a simple validation against type, the custom validator allows you to customize exactly what constitutes a valid property for your component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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