Prop Typing and Validation
Props define the interface of Vue.js components. As JavaScript is a dynamically typed language, Vue.js provides a tool we can use to validate the shape and types of props.
To validate prop types, the props
component property in its object literal form should be used (as opposed to the simpler array form).
Primitive Prop Validation
Say we want a Repeat.vue
component that takes a times
prop, as well as a content
prop. We can define the following:
<template> Â Â <div> Â Â Â Â <span v-for="r in repetitions" :key="r"> Â Â Â Â Â Â {{ content }} Â Â Â Â </span> Â Â </div> </template> <script> export default { Â Â props: ['times', 'content'], Â Â computed: { Â Â Â Â repetitions() { Â Â Â Â Â Â return Array.from({ length: this.times }); Â &...