Author -  Sai gowtham

Props vs Data in Vue.js with the help of Examples

In this tutorial, we are going to learn about the difference between props and data in vue with the help of examples.

Props

  • Props are used to pass the data to child components from parent components.

  • Props are read-only (We can’t mutate props).

  • Props are registered inside vue components by using props property.

Example:

Welcome.vue
<template>
   <h1>Welcome {{name}}</h1></template>

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

In this example, we registered name inside Welcome component, registered props can be used just like data properties inside a template.

Let’s pass the data to Welcome component.

App.vue
<template>
   <div id="app">
      <Welcome name="Gowtham"/>   </div>
</tempalte>

Here we passed data to the Welcome component by using name prop.

Data

The data we defined inside vue components are independent to that component, so that we can’t access or mutate the data from other components.

The only way to access the component data from the other components is by using props.

Data is mutable in vue.js so that if we update the data properties vue will automatically re-render your component with the updated changes.

Example:

Let’s consider we have two components named Welcome.vue and App.vue.

Welcome.vue
<template>
   <h1>Welcome {{name}}</h1></template>

<script>
   export default{
       props:['name'] //name prop is registered   }
</script>
App.vue
<template>
  <div>
    <Welcome :name="name" />    <button @click="changeName">Change name</button>
  </div>
</template>

<script>
import Welcome from "./welcome.vue";
export default {
  data: function() {
    return {
      name: "Gowtham"    };
  },
  methods: {
    changeName() {
      this.name = "Vue";
    }
  },
  components: { Welcome }
};
</script>

In the App component we passed data property name to the Welcome component using name prop.

Now if we click on Change name button the name is property is updated so that the Welcome component prop is also updated with the new value.

Have you observed one thing the data property of an App component will be the prop to the Welcome component?

Conclusion

  • Data is private to the components and we can access that data from other components with the help of props.

  • Data is mutable and props are read-only.

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