Skip to Content

How To Use Vue Props With Vue Prop Examples

Props are an essential feature in Vue.js that facilitates seamless communication between components.

By passing data from parent components to child components, Vue props enable better organization, reusability, and maintainability of your Vue.js code.

Join us on this learning journey as we explore Vue props with real-world examples, empowering you to build more dynamic and interactive applications.

No products found.

If you want to learn more about What Are Vue Views In Vue.js?

Vue Props

Vue Props are a way to pass data from a parent component to its child components. They allow communication and data flow between components in a structured manner.

What Is Props In Vue?

What Is Props In Vue? In Vue.js, props are a way to pass data from a parent component to its child components.

They allow communication and data flow between components in a structured manner.

Props are used to customize and configure child components based on the needs of the parent component.

When a parent component wants to pass data to its child component, it can bind the data to a prop defined in the child component.

The child component can then access and use the passed data within its template or script section.

Props are essentially properties of a component that are passed down from the parent to the child.

What Is The Difference Between Vue 2 And Vue 3 Props?

In Vue 2, props are declared as an array in the component’s options, specifying the names of the expected props.

The props can also be defined with additional configuration options, such as specifying the prop’s type, default value, or validator function.

These options allow for better control and validation of the incoming data.

However, Vue 3 introduced the Composition API, which provides a more flexible and modular way of organizing component logic.

With the Composition API, props are defined using the defineProps function within the setup function of a component.

This approach allows for more declarative and reactive prop definitions.

Vue 3 also introduced the reactive function, which can be used to make the props reactive, enabling automatic reactivity when the prop values change.

This simplifies the process of using props within the component’s template or script section, as they can be directly used without explicit reactivity annotations.

Vue Props Type

Vue Props Type: When defining a prop in a component’s options in Vue 2, the type option allows prop types to be specified.

The built-in prop types that are readily available include String, Number, Boolean, Array, Object, Date, Function, and Symbol.

A constructor function can also be used to generate bespoke types instead of a type.

In Vue 3, prop types are specified in a slightly different way using the Composition API.

The defineProps function is used within the setup function, and the prop types are defined using TypeScript-style type annotations.

This allows for static type checking and enhanced tooling support in Vue 3 projects.

Check out my article about Master Vue Props Function With Prop Functions Examples

Vue Props Default Value

Vue Props Default Value: Props can have default values assigned to them. A default value is used when the parent component doesn’t provide a value for the prop.

Setting default values for props ensures that components can function properly even when certain data is not passed from the parent.

In Vue 2, default values for props can be defined using the default option when declaring the prop. The default option can take a static value or a function that returns the default value.

If a function is used, it will be called every time a new instance of the component is created.

Here’s an example of defining a prop named count with a default value of 0 in Vue 2:

Vue Props Default Value 1

In Vue 3, default values are specified in a similar way using the Composition API. The default option can be set when defining props using the defineProps function.

The default value can be a static value or a function that returns the default value.

Here’s an example of defining a prop named count with a default value of 0 in Vue 3:

Vue Props Default Value 2

When a parent component doesn’t provide a value for a prop with a default value, the default value will be used.

However, if the parent component explicitly passes a value for the prop, the passed value will override the default value.

Vue Props v-model

Vue Props v-model: To enable v-model support for a prop, two additional declarations are required: modelValue and update:modelValue.

The modelValue prop holds the current value of the component, and the update:modelValue event is emitted to update the value from the child component to the parent.

Here’s an example of using v-model with a custom input component in Vue 2:

Vue Props v-model 1

In Vue 3, the v-model syntax has been updated to be more explicit and flexible.

The v-model directive can be replaced with two separate directives: :model and @update:model. This change allows for clearer separation of data binding and event handling.

Here’s an example of using the updated v-model syntax in Vue 3:

Vue Props v-model 2

By using v-model with props, changes made to the input or component value will automatically propagate to the parent component, keeping the data in sync without requiring explicit event handling.

Vue Props Array

Vue Props Array: To define a prop that accepts an array in Vue 2, the type option can be set to Array. This ensures that the received prop value is an array.

Here’s an example of defining a prop named items that accepts an array in Vue 2:

Vue Props Array 1

In Vue 3, array props can be defined similarly using the Composition API and the defineProps function.

Here’s an example of defining a prop named items that accepts an array in Vue 3:

Vue Props Array 2

Once the array prop is defined, the parent component can pass an array to the child component using the prop’s name.

The child component can then access and manipulate the array as needed.

Vue Props Object

Vue Props Object: To define a prop that accepts an object in Vue 2, the type option can be set to Object. This ensures that the received prop value is an object.

Here’s an example of defining a prop named config that accepts an object in Vue 2:

Vue Props Object 1

In Vue 3, object props can be defined similarly using the Composition API and the defineProps function.

Here’s an example of defining a prop named config that accepts an object in Vue 3:

Vue Props Object 2

Once the object prop is defined, the parent component can pass an object to the child component using the prop’s name.

The child component can then access and use the object’s properties as needed.

Vue Props Proxy

Vue Props Proxy: By default, when a prop is received in Vue 3, it is a read-only reactive object.

However, by using the toRef or toRefs function, the prop object can be transformed into a reactive proxy, allowing for bidirectional reactivity between the parent and child components.

Here’s an example of creating a reactive proxy for a prop object in Vue 3:

Vue Props Proxy

In the example, the toRef function is used to create a reactive reference to the user prop. The userRef can then be accessed and modified as a regular reactive object.

Don´t forget to check out my article about How To Get A Junior Software Developer Job?

Vue Props Validator

Vue Props Validator: In Vue 2, prop validators can be defined using the validator option when declaring the prop.

The validator option expects a function that takes the prop value as its argument and returns a boolean value indicating whether the prop is valid.

Here’s an example of defining a prop named age with a custom validator in Vue 2:

Vue Props Validator 1

In Vue 3, prop validators are defined in a similar way using the Composition API and the defineProps function.

Here’s an example of defining a prop named age with a custom validator in Vue 3:

Vue Props Validator 2

The prop validator function receives the prop value as its argument and should return true if the value is considered valid. If the validator returns false, a warning will be issued in the console.

Vue Props Composition API

Vue Props Composition API: In Vue 3, the Composition API provides a more modular and flexible way of working with props.

Instead of declaring props in the options object, the defineProps function is used within the setup function to define and configure props.

Here’s an example of using the Composition API to define props in Vue 3:

Vue Props Composition API 1

In the example, the defineProps function is used to define the count and message props. The prop definitions include the prop types, default values, and other configuration options.

The defined props can then be accessed within the setup function using the props object. The props are reactive by default, allowing for automatic reactivity when their values change.

Vue 3 Props

Vue 3 Props To Reactive

Vue 3 Props To Reactive: In Vue 3, props can be transformed into reactive references using the toRef function from the Composition API.

This allows for automatic reactivity when the prop values change, simplifying the usage of props within the component.

To create a reactive reference to a prop in Vue 3, the toRef function is used within the setup function.

The toRef function takes two arguments: the props object and the name of the prop to be transformed into a reactive reference.

Here’s an example of creating a reactive reference to a prop named message in Vue 3:

Vue 3 Props

In the example, the toRef function is used to create a reactive reference messageRef to the message prop. The messageRef can then be accessed and used as a regular reactive object.

Vue 3 Props Watch

Vue 3 Props Watch: To watch a prop for changes in Vue 3, the watch function is used within the setup function.

The watch function takes two arguments: the prop to be watched and a callback function that will be called when the prop value changes.

Here’s an example of watching a prop named count in Vue 3:

Vue 3 Props Watch

In the example, the watch function is used to watch the count prop.

Whenever the count prop value changes, the callback function will be called with the new value and the old value as arguments.

Don´t forget to check out my article about How To Use Vue Force Update With Forceupdate Example

Vue 3 Props Setup

Vue 3 Props Setup: In Vue 3, the setup function in the Composition API provides a convenient way to access and configure props.

The setup function allows for defining reactive references to props, watching prop changes, and performing other setup logic for the component.

To work with props in the setup function in Vue 3, the defineProps function is used to define the props and access their values.

The props can then be used and manipulated within the setup function as needed.

Here’s an example of using the setup function to work with props in Vue 3:

Vue 3 Props Setup

In the example, the defineProps function is used to define the name and age props. The props object is then accessed within the setup function using the props parameter.

Vue Pass Function As Prop

Vue Pass Function As Prop With Parameter

To pass a function as a prop in Vue, the parent component can define a method and pass it as a prop to the child component.

The child component can then invoke the passed function when needed, optionally passing parameters to the function.

Here’s an example of passing a function as a prop with a parameter from the parent component to the child component:

Parent component:

Vue Pass Function As Prop 1

Child component:

Vue Pass Function As Prop 2

In this example, the parent component defines a method named handleButtonClick that takes a parameter. The handleButtonClick method logs the parameter to the console.

The parent component passes the handleButtonClick method as the onButtonClick prop to the child component.

The child component defines a button and binds a click event to the handleClick method.

When the button in the child component is clicked, the handleClick method is called.

It then invokes the onButtonClick prop, passing the parameter ‘Hello, world!’. The parent component’s handleButtonClick method is executed with the passed parameter.

Vue Access Props In Method

Vue Access Props In Method Example

Vue Access Props In Method Example: To access props within a method in Vue, the props can be accessed directly using the this keyword, as props are available as properties of the component instance.

Here’s an example of accessing props within a method in Vue:

Vue Access Props In Method

In this example, the component has a prop named message, which is of type String and is required. The prop value is accessed within the updateMessage method using this.message.

The updateMessage method logs the current value of the message prop to the console. This demonstrates how props can be accessed and used within component methods.

Vue Pass Component As Prop

Vue Pass Component As Prop Example

Let’s consider an example where we have a parent component called ParentComponent and a child component called ChildComponent.

The goal is to pass ChildComponent as a prop to ParentComponent and render it dynamically.

First, let’s define the ChildComponent:

Vue Pass Component As Prop 1

Now, let’s define the ParentComponent and pass ChildComponent as a prop:

Vue Pass Component As Prop 2

In the ParentComponent, we import the ChildComponent and register it as a component.

We then define a parentMessage data property that holds the message we want to pass to the ChildComponent.

Inside the template, we use the child-component tag to render the ChildComponent and bind the message prop to the parentMessage property.

When the ParentComponent is rendered, the ChildComponent will be dynamically rendered as well, and the message prop will be set to the value of parentMessage.

This allows the ChildComponent to display the message provided by the parent.

Don´t forget to check out my article about How To Use Vue Watch Deep (Deep Watch Examples)

Vue Pass Component As Prop TypeScript

Vue Pass Component As Prop TypeScript: If you’re using TypeScript in your Vue.js project, you can also specify the prop types explicitly.

Here’s an example of how you can define the prop types for ChildComponent and ParentComponent using TypeScript:

Vue Pass Component As Prop TypeScript 1

In this TypeScript example, we use the defineComponent function from the vue package to define the components.

We define interfaces ChildProps and ParentProps to specify the prop types for ChildComponent and ParentComponent, respectively.

Inside each component, we use the props option to define the prop types.

By using TypeScript, we can benefit from static type checking and better code editor support, ensuring that the props passed to the components adhere to the specified types.

Vue 3 Pass Props To Child Component

Vue 3 Pass Props To Child Component Example

Let’s illustrate an example where we have a parent component called ParentComponent and a child component called ChildComponent in Vue 3.

We’ll pass props from the parent to the child component.

First, let’s define the ChildComponent:

Vue 3 Pass Props To Child Component 1

The ChildComponent is defined using the Composition API. It accepts a prop named message of type String and displays it in the template.

Now, let’s define the ParentComponent and pass the props to ChildComponent:

Vue 3 Pass Props To Child Component 2

In the ParentComponent, we import the ChildComponent and register it as a component.

We then define a parentMessage data property that holds the message we want to pass to the ChildComponent.

Inside the template, we use the child-component tag to render the ChildComponent and bind the message prop to the parentMessage property.

When the ParentComponent is rendered, the ChildComponent will receive the message prop with the value of parentMessage.

Vue 3 Pass Props To Child Component Composition API

With the Composition API in Vue 3, we have more flexibility when passing props to child components.

We can use the setup function to handle props and define reactive variables. Here’s an example:

Vue 3 Pass Props To Child Component Composition API 1

In this example, we define the parentMessage prop in the props option. In the setup function, we create a reactive variable childMessage using the ref function.

We also use the watchEffect function to update childMessage whenever the parentMessage prop changes.

By returning childMessage from the setup function, it becomes accessible in the template of the ChildComponent.

Using the Composition API allows us to easily react to changes in props and perform custom logic based on them, providing more control and flexibility in handling prop values within the component.

Hopefully, you now have a better understanding of how you can use Vue Props.

Understanding how you can use Vue Props is gonna help you with communication and data flow between components in a structured manner.