Vue.js Props – Passing Data to Components
In Vue.js, props (short for properties) are the mechanism by which a parent component can pass data down to its child components. Props make components reusable and flexible, allowing you to configure them differently in different contexts.
1. What Are Props?
Props are essentially input parameters for components. By defining props in a child component, you can accept dynamic data from the parent. This is the most common way to communicate from a parent to a child component.
In the child component, you define props like this:
props: ['name']
Now, the child can access the name prop as reactive data.
2. Dynamic Props
Props can be dynamic, meaning they automatically update if the parent changes the bound value. For example:
Whenever parentMessage changes, the child component automatically receives the updated value.
3. Prop Types and Validation
Vue allows you to specify the type of a prop and whether it’s required. This helps prevent bugs by validating the data passed into a component.
props: {
title: {
type: String,
required: true
},
likes: {
type: Number,
default: 0
}
}You can use type, required, and default to make your components safer and easier to maintain.
4. Default Prop Values
Props can have default values if the parent does not provide them. This ensures that components always have meaningful data.
In the child component, you might define a default for another prop:
props: {
likes: {
type: Number,
default: 0
}
}5. Passing Objects as Props
Props can be objects or arrays. This is useful when passing multiple related pieces of data.
In the child, you can access the object directly: props.user.name.
6. Using Props in Computed Properties
Props are reactive, so you can use them inside computed properties to create derived values.
In the child component:
computed: {
uppercaseTitle() {
return this.title.toUpperCase();
}
}7. Using Props in Methods
You can also use props inside methods to manipulate data or trigger actions.
Child method example:
methods: {
greet() {
alert('Hello, ' + this.name);
}
}8. One-Way Data Flow
Props are one-way: changes inside the child do not affect the parent. This prevents unexpected side effects. If you need to update the parent, emit an event instead.
9. Practical Example – Product Card
Here is a reusable product card that accepts props for title, price, and inStock.
10. Tips for Using Props
- Always validate prop types when possible.
- Provide default values for optional props.
- Props should be one-way: child should not mutate them.
- Use events to notify parent about changes.
- Keep props focused on data that the child component truly needs.
Conclusion
Props are essential for creating reusable and maintainable Vue.js components. By understanding how to pass data from parent to child, validate props, and combine them with computed properties and methods, you can create dynamic, reactive, and robust applications. Using VueCompiler in Next.js allows you to demonstrate these concepts interactively.