VueJS - Props
Using Props to Pass Data to Components
Props are a mechanism for passing data from a parent component to a child component. This guide covers the basics of using props in VueJS, including defining and passing props, using type validation, and setting default values.
Key Points:
- Props are used to pass data from parent components to child components.
- Props can be defined with type validation and default values.
- Props are read-only and should not be mutated inside the child component.
Defining and Passing Props
Basic Usage
Define props in a child component using the props
option and pass data from the parent component:
// JavaScript
Vue.component('child-component', {
props: ['message'],
template: '{{ message }}'
});
const app = new Vue({
el: '#app',
data: {
parentMessage: 'Hello from parent!'
}
});
Multiple Props
Define and pass multiple props:
// JavaScript
Vue.component('child-component', {
props: ['message', 'name'],
template: '{{ message }} from {{ name }}'
});
const app = new Vue({
el: '#app',
data: {
parentMessage: 'Hello',
parentName: 'VueJS'
}
});
Type Validation
Basic Type Validation
Define props with type validation to ensure they receive the correct data type:
// JavaScript
Vue.component('child-component', {
props: {
message: String,
count: Number,
isActive: Boolean
},
template: '{{ message }} - {{ count }} - {{ isActive }}'
});
const app = new Vue({
el: '#app',
data: {
parentMessage: 'Hello',
parentCount: 10,
parentIsActive: true
}
});
Advanced Type Validation
Define props with advanced type validation and default values:
// JavaScript
Vue.component('child-component', {
props: {
message: {
type: String,
required: true
},
count: {
type: Number,
default: 0
},
user: {
type: Object,
default: function() {
return { name: 'Anonymous' };
}
}
},
template: '{{ message }} - {{ count }} - {{ user.name }}'
});
const app = new Vue({
el: '#app',
data: {
parentMessage: 'Hello',
parentCount: 10,
parentUser: { name: 'VueJS' }
}
});
Passing Static and Dynamic Props
Static Props
Pass static props directly in the template:
// JavaScript
Vue.component('child-component', {
props: ['message'],
template: '{{ message }}'
});
const app = new Vue({
el: '#app'
});
Dynamic Props
Pass dynamic props using v-bind:
// JavaScript
Vue.component('child-component', {
props: ['message'],
template: '{{ message }}'
});
const app = new Vue({
el: '#app',
data: {
parentMessage: 'Hello dynamic prop!'
}
});
Best Practices
Follow these best practices when using props in VueJS:
- Use Meaningful Prop Names: Use descriptive and meaningful names for your props to make your code more readable and maintainable.
- Validate Prop Types: Define prop types and use type validation to ensure your components receive the correct data.
- Avoid Mutating Props: Props are read-only. Avoid mutating props directly inside child components. Use computed properties or methods to handle derived state.
- Set Default Values: Define default values for props to ensure your components work correctly even if some props are not provided.
- Document Props: Document the props for each component to make your code easier to understand and maintain.
Example Application
Here is an example application that demonstrates the basics of using props in VueJS:
Props Example
Summary
This guide provided an overview of using props in VueJS, including defining and passing props, using type validation, setting default values, and following best practices. By understanding and utilizing these features, you can create robust and maintainable VueJS components.