Author -  Sai gowtham

Intro to Vue.JS Props with examples

In this tutorial, we are going to learn about props in vue.js apps with the help of examples.

Props

In Vue.js props helps us to pass the data from parent components to its child components.

Registering props

To use the props in Vue components first we need to register the props.

Let’s create a new component called Button.

Button.vue
<template>
  <button>{{name}}</button></template>

<script>
 export default{
     props:['name'] }
</script>

In the above code, we have registered a prop called name inside the props array. The registered props can be used inside the template just like data properties.

Passing the data to the props

Now, we can pass the data to that prop as a custom HTML name attribute.

Post.vue
<template>
  <div>
    <h1>My first post</h1>
    <p>This a big post helps us to learn vue props</p>
    <my-button name="Share"></my-button>    <my-button name="Like"></my-button>    <my-button name="Comment"></my-button>  </div>
</template>

<script>
  import Button from './Button.vue'
  export default{
    components:{
        'my-button':Button
    }
  }
</script>

passing-data-vue-props

Passing functions to the props

Let’s register a second prop to our Button component.

Button.vue
<template>
  <button @click="handleClick">{{ name }}</button></template>

<script>
export default {
  props: ["name", "handleClick"]};
</script>

Here we registered a handleClick prop which is attached to @click event to the button. Now we need to pass the function to that prop.

Post.vue
<template>
  <div>
    <h1>My first post</h1>
    <p>This a big post helps us to learn vue props</p>
    <my-button name="Share" :handleClick="shareMe"></my-button>  </div>
</template>

<script>
import Button from "./components/Button.vue";
export default {
  components: {
    "my-button": Button
  },
  methods: {
    shareMe: function() {
        //write your logic
      console.log("share");
    }
  }
};
</script>

Inside the template we passed the shareMe function to our :handleClick prop.

for dynamic values we need to use (colon) :propname instead of propname otherwise Vue treated it as JavaScript string.

passing-functions-as-props

Validating props

So far we are registering props by using an array syntax but there is a second way to register and validate props by using object syntax.

Let’s validate our props present in Button component.

Button.vue
<template>
  <button @click="handleClick">{{ name }}</button>
</template>

<script>
export default {
  props: {    name:{      required:true,      type:String    },    handleClick:{      required:true,      type:Function    }  }};
</script>

Here we added two properties which are required and type of data the prop is accepting.

Now if we fail to pass the data to that prop Vue.js shows us an error inside our browser’s console.

Available Type checks

The type can be one of the following native constructors:

String
Number
Boolean
Array
Object
Date
Function
Symbol

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY