Components let you build small, reusable parts of a webpage. They help organize your code and make it easier to manage.
Defining components in Vue
<script setup> // Define your component logic here </script> <template> <!-- Your HTML structure here --> </template> <style scoped> /* Your component styles here */ </style>
<script setup> for a simple and clean way to write component logic in Vue 3.4+.scoped attribute in <style> makes styles apply only to this component.<script setup> const message = 'Hello from component!' </script> <template> <p>{{ message }}</p> </template>
<script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value++ } </script> <template> <button @click="increment">Clicked {{ count }} times</button> </template>
title prop to display a heading.<script setup> const props = defineProps({ title: String }) </script> <template> <h1>{{ title }}</h1> </template>
This component shows a button that counts how many times you click it. It uses ref to keep track of the count. The button has an accessible label and changes color when hovered or focused. The styles are scoped so they only affect this component.
<script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value++ } </script> <template> <section> <h2>Counter Component</h2> <button @click="increment" aria-label="Increment counter"> Clicked {{ count }} times </button> </section> </template> <style scoped> section { padding: 1rem; border: 2px solid #4a90e2; border-radius: 0.5rem; max-width: 200px; text-align: center; font-family: Arial, sans-serif; } button { background-color: #4a90e2; color: white; border: none; padding: 0.5rem 1rem; font-size: 1rem; border-radius: 0.25rem; cursor: pointer; } button:hover, button:focus { background-color: #357ABD; outline: none; } </style>
Always use ref or reactive to make data reactive in Vue components.
Use @click to listen for user clicks on buttons or other elements.
Scoped styles help keep your component styles from affecting other parts of the page.
Components are reusable building blocks for your Vue app.
Use <script setup> and <template> to define component logic and layout.
Props let you pass data into components, and reactive variables track changing data.
Practice
Solution
Step 1: Understand what components do in Vue
Components are small, reusable parts of the user interface that can be used multiple times in an app.Step 2: Identify the correct purpose
Among the options, only creating reusable UI pieces matches the role of components.Final Answer:
To create reusable pieces of UI that can be used throughout the app -> Option BQuick Check:
Components = reusable UI blocks [OK]
- Confusing components with styling or backend logic
- Thinking components handle database connections
- Believing components only work on the server side
<script setup>?Solution
Step 1: Recognize the
In Vue 3,<script setup>syntax<script setup>is a special syntax that allows direct use of variables without export default.Step 2: Check which option uses
<script setup> const message = 'Hello' </script> <template> <p>{{ message }}</p> </template> declares a variable inside<script setup>correctly<script setup>and uses it in the template, which is correct.Final Answer:
<script setup> const message = 'Hello' </script> <template> <p>{{ message }}</p> </template> -> Option AQuick Check:
<script setup>uses direct variable declaration [OK]
- Using export default inside
- Declaring variables outside
- Placing <script> after <template> incorrectly
<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
count.value++
}
</script>
<template>
<button @click="increment">Clicked {{ count }} times</button>
</template>Solution
Step 1: Understand ref and reactivity
The variablecountis a reactive reference starting at 0. Usingcount.value++updates it reactively.Step 2: Check the template interpolation
The template uses {{ count }}, which Vue unwraps automatically to show the current value ofcount.Final Answer:
A button showing 'Clicked 0 times' initially, increasing count on each click -> Option AQuick Check:
ref count starts at 0 and updates on click [OK]
- Thinking count starts at 1 instead of 0
- Expecting literal 'count' string instead of value
- Assuming count is not reactive without .value
<script setup>
const props = defineProps({
title: String
})
function showTitle() {
console.log(props.title.toUpperCase())
}
showTitle()
</script>
<template>
<h1>{{ title }}</h1>
</template>Solution
Step 1: Analyze prop definition and usage
defineProps({ title: String })declarestitlewithoutrequired: trueor default value, soprops.titlecan beundefined.Step 2: Locate the error in JS code
InshowTitle(),props.title.toUpperCase()assumestitleis a string, but callingtoUpperCase()onundefinedcauses a runtime error.Final Answer:
UsingtoUpperCase()on a possibly undefined prop -> Option DQuick Check:
Validate props before string operations [OK]
- Applying string methods to props that may be undefined
- Forgetting to import defineProps (though auto-imported in some setups)
- Assuming props are reactive variables directly
user prop object with name and age, and displays "Name: [name], Age: [age]". Which is the best way to define and use this prop in a <script setup> component?Solution
Step 1: Define the prop as an object with expected properties
The propuseris an object containingnameandage, so define it as an Object type and mark required if needed.Step 2: Access the nested properties correctly in the template
Useprops.user.nameandprops.user.ageto display the values inside the template.Final Answer:
Define user as an object prop and access nested properties via props.user.name and props.user.age -> Option CQuick Check:
Object prop with nested access = <script setup> const props = defineProps({ user: { type: Object, required: true } }) </script> <template> <p>Name: {{ props.user.name }}, Age: {{ props.user.age }}</p> </template> [OK]
- Defining nested fields as separate props instead of one object
- Destructuring props incorrectly in
- Using defineProps with wrong syntax for objects
Ready to prove it?
Pass the test and this pattern moves unknown
