📖 Scenario: You are building a simple Vue 3 component that shows a greeting message. You will use the <script setup> to define the component's data.
🎯 Goal: Create a Vue 3 component using the Composition API with <script setup> that defines a reactive variable message with the value 'Hello, Vue!'. The message will be automatically available for use in the template.
📋 What You'll Learn
Use the Composition API with <script setup>
Import and use ref from 'vue'
Create a reactive variable called message with the exact value 'Hello, Vue!'
Make the message variable available to the template (automatic in <script setup>)
Use the message variable in the template with interpolation
💡 Why This Matters
🌍 Real World
Vue 3's <code><script setup></code> is the modern way to write components. It helps organize code clearly and makes it easier to manage reactive data.
💼 Career
Understanding the <code><script setup></code> and Composition API is essential for modern Vue development jobs, as many projects use this pattern for better scalability and maintainability.
Progress0 / 4 steps
1
Create the Vue component skeleton
Write the basic Vue 3 component structure with <template> and <script setup> tags. Inside the template, add a <div> element that will later display the message.
Vue
Hint
Start by writing the template with a <div> that uses {{ message }} interpolation. Then add the <script setup> block below.
2
Import ref from Vue
Inside the <script setup> block, write an import statement to import ref from the 'vue' package.
Vue
Hint
Use the ES module import syntax: import { ref } from 'vue'.
3
Create the reactive message variable
Inside the <script setup> block, create a reactive variable called message using ref and set its initial value to the string 'Hello, Vue!'.
Vue
Hint
Use const message = ref('Hello, Vue!') to create the reactive variable.
4
Make the message variable available for template use
In the <script setup> block, ensure the message variable is available to the template by simply defining it in the <script setup> context (no explicit return needed). Confirm the template uses {{ message }} to display the message.
Vue
Hint
In <script setup>, variables are automatically exposed to the template. Just make sure {{ message }} is in the template.
Practice
(1/5)
1. What is the main purpose of the setup function in a Vue 3 component?
easy
A. To register global components
B. To define reactive data and functions before the component renders
C. To handle routing between pages
D. To style the component using CSS
Solution
Step 1: Understand the role of setup in Vue 3
The setup function is where you declare reactive data and functions that the component will use.
Step 2: Compare with other options
Styling, routing, and global registration are handled elsewhere, not in setup.
Final Answer:
To define reactive data and functions before the component renders -> Option B
Quick Check:
Setup function = reactive data + functions [OK]
Hint: Remember: setup is for data and logic, not styling or routing [OK]
Common Mistakes:
Confusing setup with styling or routing
Thinking setup runs after rendering
Assuming setup registers global components
2. Which of the following is the correct way to declare a reactive variable inside the setup function using Vue 3 Composition API?
easy
A. const count = useState(0);
B. const count = reactive(0);
C. const count = reactive({ value: 0 });
D. const count = ref(0);
Solution
Step 1: Identify correct reactive declaration for simple values
For simple reactive variables like numbers, ref is used, e.g., const count = ref(0);.
Step 2: Check other options
reactive is for objects, not primitives; useState is not a Vue function.
Final Answer:
const count = ref(0); -> Option D
Quick Check:
Simple reactive variable = ref() [OK]
Hint: Use ref() for single values, reactive() for objects [OK]
Common Mistakes:
Using reactive() for primitive values
Trying to use non-existent useState()
Wrapping primitive in reactive({ value: ... }) unnecessarily
C. Cannot assign to ref variable directly; should use count.value
D. increment function is not declared correctly
Solution
Step 1: Check how ref variables are updated
Ref variables hold their value inside .value. Direct assignment like count = count + 1 is invalid.
Step 2: Correct way to update ref
Use count.value = count.value + 1 or count.value++ to update.
Final Answer:
Cannot assign to ref variable directly; should use count.value -> Option C
Quick Check:
Update ref with .value [OK]
Hint: Always update ref with .value, not direct assignment [OK]
Common Mistakes:
Assigning directly to ref variable
Forgetting to use .value when updating
Confusing reactive and ref update syntax
5. You want to create a Vue 3 component using <script setup> that has a reactive object with properties name and age. Which code correctly defines this reactive object and updates age by 1 inside setup?
hard
A.
import { reactive } from 'vue';
const person = reactive({ name: 'Alice', age: 30 });
person.age++;
B.
import { ref } from 'vue';
const person = ref({ name: 'Alice', age: 30 });
person.age++;
C.
import { reactive } from 'vue';
const person = reactive({ name: 'Alice', age: 30 });
person = { ...person, age: person.age + 1 };
D.
import { ref } from 'vue';
const person = ref({ name: 'Alice', age: 30 });
person.value.age = person.age + 1;
Solution
Step 1: Choose correct reactive type for object
Use reactive for objects to make all properties reactive.
Step 2: Update property correctly
With reactive, update properties directly like person.age++. Using ref with objects requires accessing .value.
Final Answer:
import { reactive } from 'vue';
const person = reactive({ name: 'Alice', age: 30 });
person.age++; -> Option A
Quick Check:
Reactive object updated directly =
import { reactive } from 'vue';
const person = reactive({ name: 'Alice', age: 30 });
person.age++;
[OK]
Hint: Use reactive() for objects, update properties directly [OK]
Common Mistakes:
Using ref for objects without .value access
Reassigning reactive object instead of updating property
Trying to increment property on ref object without .value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.