Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
👋 Hi! I'm Dastan, author of Hack Frontend. Always open to professional networking => connect with me on LinkedIn.
Props are a way to pass data from parent component to child in Vue.js. They define component interface, making it reusable and configurable without hard binding to internal state.
export default {
name: "UserCard",
props: {
// required, String type
name: {
type: String,
required: true
},
// optional, Number type, default value
age: {
type: Number,
default: 18
},
// simple validation
isActive: {
type: Boolean,
validator: value => typeof value === "boolean"
}
}
};
import { defineComponent } from "vue";
export default defineComponent({
name: "UserCard",
props: {
name: { type: String, required: true },
age: { type: Number, default: 18 },
isActive: Boolean
},
setup(props) {
// props.name, props.age, props.isActive available here
return { props };
}
});
Don't modify props inside component:
props are incoming data; for internal state use data or ref in Composition API.
<template>
<div class="user-card">
<h2>{{ name }}</h2>
<p>Age: {{ age }}</p>
<p v-if="isActive">Status: Active</p>
</div>
</template>
<script>
export default {
props: ["name", "age", "isActive"]
};
</script>
Note:
In Composition API you can destructure props immediately in setup: setup({ name, age, isActive }) { /* … */ }
Define props property in component with type and rules (required, default, validator).
In parent component use attributes: <UserCard name="John" :age="30" :isActive="true" />
Access props as reactive variables: {{ name }}, {{ age }}.
More in official Vue documentation: Vue Props Documentation