Passing Data with Props

Tutorial 2 of 5

1. Introduction

The goal of this tutorial is to guide you through the process of passing data with props in Vue.js. Props, short for properties, are a way to pass data from parent components to child components in Vue. This is a fundamental concept in Vue and understanding it will allow you to build complex, data-driven Vue applications.

By the end of this tutorial, you will learn:

  • The concept of props in Vue
  • How to define and use props
  • How to pass data from a parent component to a child component using props

Prerequisites:

  • Basic understanding of Vue.js and its component system
  • Familiarity with JavaScript

2. Step-by-Step Guide

What are props?

Props are custom attributes that you can register on a component. When a value is passed to a prop attribute, it becomes a property on that component instance.

Passing and Using Props

To pass a prop to a child component, you add it as an attribute in the child component's tag in the parent's template. Then, in the child component, you declare the prop in the props option.

Let's see an example:

ParentComponent.vue:

<template>
  <ChildComponent :message="parentMessage" />
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent,
  },
  data() {
    return {
      parentMessage: 'Hello from parent',
    };
  },
};
</script>

ChildComponent.vue:

<template>
  <p>{{ message }}</p>
</template>

<script>
export default {
  props: ['message'],
};
</script>

In the ParentComponent, we pass the parentMessage to the ChildComponent using the :message prop. In the ChildComponent, we declare the message prop and use it in the template.

3. Code Examples

Let's dig deeper with more examples:

Example 1: Passing a Number

ParentComponent.vue:

<template>
  <ChildComponent :num="123" />
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent,
  },
};
</script>

ChildComponent.vue:

<template>
  <p>{{ num }}</p>
</template>

<script>
export default {
  props: ['num'],
};
</script>

In this example, we pass a number to the ChildComponent using the :num prop.

Example 2: Passing an Object

ParentComponent.vue:

<template>
  <ChildComponent :user="user" />
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent,
  },
  data() {
    return {
      user: {
        name: 'John Doe',
        email: 'john@doe.com',
      },
    };
  },
};
</script>

ChildComponent.vue:

<template>
  <p>{{ user.name }} - {{ user.email }}</p>
</template>

<script>
export default {
  props: ['user'],
};
</script>

Here, we pass an object to the ChildComponent using the :user prop.

4. Summary

In this tutorial, we learned about props in Vue and how to use them to pass data from parent components to child components. We saw how to pass different types of data, such as strings, numbers, and objects.

Next steps for learning would be to understand how to pass data from child components to parent components using custom events and how to validate props.

Additional resources:
- Vue.js Guide on Props
- Vue.js API on Props

5. Practice Exercises

Exercise 1: Create a parent component and a child component. Pass a string from the parent to the child using a prop and display it in the child's template.

Exercise 2: Modify the parent component to pass a number and an object to the child component using props. Display the number and the object's properties in the child's template.

Exercise 3: Create a component that receives an array of objects via props. Display each object's properties in the component's template.

Solutions:

  • The solutions will vary based on the specific data passed and the structure of your components. The key is to correctly define and use props to pass data from parent to child components.

Tips for further practice:

  • Experiment with passing different types of data using props.
  • Practice validating the data passed via props using Vue's prop validation features.