Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple data types for VueJs Props?

This error got me when passing different values to the component.

enter image description here

like image 920
Harsha Sampath Avatar asked Dec 04 '18 08:12

Harsha Sampath


People also ask

How do I use Vue data props?

To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.

How do you pass a component as a prop Vue?

To pass a component as props and use it in a child component in Vue. js, we can use the component component. to add the component component. We set the is prop to the childComponent string which has the name of the component we want to render.


2 Answers

Here is the solution I found.

props: {    value: [Number, String, Array] } 
like image 136
Harsha Sampath Avatar answered Oct 12 '22 19:10

Harsha Sampath


The syntax with a pipe (Number | String), like proposed in the accepted answer, does not actually work. Here is a more detailed solution with examples:

Type-Check, Not Required Prop

Use of the following syntax to type check a prop:

props: {   username: {     type: [ String, Number ]   } } 

Here is a live example of a property with type check:

Vue.config.devtools = false;  Vue.config.productionTip = false;    Vue.component('test-component', {    name: 'TestComponent',    props: {      username: {        type: [ String, Number ]      }    },    template: `<div>username: {{ username }}</div>`  });    new Vue({ el: '#app' });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>    <div id="app">    <!-- valid: String -->    <test-component :username="'user 38'"></test-component>        <!-- valid: Number -->    <test-component :username="59"></test-component>        <!-- valid: null is valid, it is not required -->    <test-component :username="null"></test-component>      <!-- valid: missing property is valid, it is not required -->    <test-component></test-component>      <!-- invalid: Array -->    <test-component :username="['test', 456]"></test-component>  </div>

Type-Check, Required Prop & Custom Validator

Use the following syntax to type check a required property together with a custom validator.

props: {   username: {     type: [ String, Number ],     required: true, // optional     validator: item => item !== '123' // optional   } } 

Here is a live example of a required property together with a custom validator:

Vue.config.devtools = false;  Vue.config.productionTip = false;    Vue.component('test-component', {    name: 'TestComponent',    props: {      username: {        type: [ String, Number ],        required: true,        validator: item => item !== '123'      }    },    template: `<div>username: {{ username }}</div>`  });    new Vue({ el: '#app' });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>    <div id="app">    <!-- valid: String -->    <test-component :username="'user 38'"></test-component>        <!-- valid: Number -->    <test-component :username="59"></test-component>        <!-- invalid: Array -->    <test-component :username="['test', 456]"></test-component>        <!-- invalid: String, but disallowed by custom validator -->    <test-component :username="'123'"></test-component>        <!-- invalid: null property, it is required though -->    <test-component :username="null"></test-component>      <!-- invalid: missing required prop -->    <test-component></test-component>  </div>
like image 24
ssc-hrep3 Avatar answered Oct 12 '22 20:10

ssc-hrep3