Prop types and validation
📖 Scenario: You are building a simple Vue 3 component that displays user profile information. You want to ensure the component receives the correct types of data using props with validation.
🎯 Goal: Create a Vue 3 component named UserProfile that accepts props with specific types and validation rules. The component should accept a name prop as a required string, an age prop as an optional number, and a email prop as a required string with a custom validator to check it contains an '@' character.
📋 What You'll Learn
Use Vue 3 Composition API with
<script setup>Define props with types and validation
Make
name a required string propMake
age an optional number propMake
email a required string prop with a custom validator checking for '@'Render the props inside the template
💡 Why This Matters
🌍 Real World
Validating props ensures components receive the right data types and values, preventing bugs and improving reliability in real Vue applications.
💼 Career
Understanding prop types and validation is essential for frontend developers working with Vue to build robust, maintainable UI components.
Progress0 / 4 steps