📖 Scenario: You are building a simple Vue app that shows a greeting message to users. The message should update automatically when the data changes.
🎯 Goal: Create a Vue component that uses mustache syntax to display a greeting message stored in the component's data.
📋 What You'll Learn
Use Vue 3 with the Composition API and <script setup>
Create a reactive variable called greeting with the value 'Hello, Vue learner!'
Use mustache syntax {{ greeting }} in the template to show the greeting
Ensure the component is standalone and can be rendered directly
💡 Why This Matters
🌍 Real World
Text interpolation is used in many web apps to show dynamic data like user names, messages, or live updates.
💼 Career
Understanding how to bind data to the UI with mustache syntax is a fundamental skill for Vue developers and frontend engineers.
Progress0 / 4 steps
1
Create the greeting data variable
In the <script setup> section, import ref from 'vue' and create a reactive variable called greeting with the value 'Hello, Vue learner!'.
Vue
Hint
Use ref to create a reactive variable in Vue 3 Composition API.
2
Add the template with mustache syntax
In the <template> section, add a <p> tag that displays the greeting variable using mustache syntax {{ greeting }}.
Vue
Hint
Use double curly braces {{ }} to show reactive data in Vue templates.
3
Add a button to change the greeting
Below the <p> tag, add a <button> that changes the greeting variable to 'You clicked the button!' when clicked. Use the @click event with an inline arrow function.
Vue
Hint
Use @click with an arrow function to update reactive data.
4
Make the component standalone
Add the export default { block with name: 'GreetingComponent' and standalone: true below the <script setup> code to make the component standalone.
Vue
Hint
Standalone components can be used directly without modules.
Practice
(1/5)
1. What does the mustache syntax {{ message }} do in a Vue template?
easy
A. It displays the value of the variable message inside the HTML.
B. It creates a new variable called message.
C. It comments out the text inside the braces.
D. It runs a JavaScript function named message.
Solution
Step 1: Understand mustache syntax purpose
The mustache syntax {{ }} is used in Vue to insert data values into HTML templates.
Step 2: Identify what {{ message }} does
It shows the current value of the variable named message in the rendered HTML.
Final Answer:
It displays the value of the variable message inside the HTML. -> Option A
Quick Check:
Mustache syntax = display variable value [OK]
Hint: Mustache {{ }} always shows variable content in HTML [OK]
Common Mistakes:
Thinking it creates variables
Assuming it runs functions
Confusing it with comments
2. Which of the following is the correct way to display a variable title inside a Vue template using mustache syntax?
easy
A. [[ title ]]
B. { title }
C. {{ title }}
D. {% title %}
Solution
Step 1: Recall Vue interpolation syntax
Vue uses double curly braces {{ }} to insert variables into templates.
Step 2: Match the correct syntax
Only {{ title }} matches Vue's mustache syntax for interpolation.
3. Given the Vue data: { name: 'Anna' }, what will be rendered by this template?
<div>Hello, {{ name }}!</div>
medium
A.
Hello, {{ name }}!
B.
Hello, Anna!
C.
Hello, name!
D.
Hello, !
Solution
Step 1: Identify the data value
The variable name has the value 'Anna'.
Step 2: Understand interpolation output
The mustache syntax replaces {{ name }} with 'Anna' in the rendered HTML.
Final Answer:
<div>Hello, Anna!</div> -> Option B
Quick Check:
Variable value replaces {{ name }} [OK]
Hint: Mustache {{ }} shows variable value, not the code itself [OK]
Common Mistakes:
Expecting to see {{ name }} literally
Confusing variable name with its value
Leaving interpolation empty
4. What is wrong with this Vue template snippet?
<div>{{ user.name </div>
medium
A. Mustache syntax cannot access object properties.
B. Variable user is not defined.
C. Interpolation must use square brackets instead of curly braces.
D. Missing closing curly braces for interpolation.
Solution
Step 1: Check mustache syntax completeness
The snippet has only one closing curly brace; mustache syntax requires double closing braces }}.
Step 2: Confirm correct syntax for interpolation
Proper interpolation should be {{ user.name }} with both opening and closing double braces.
Final Answer:
Missing closing curly braces for interpolation. -> Option D
Quick Check:
Mustache needs {{ and }} to work [OK]
Hint: Always close mustache with double braces }} [OK]
Common Mistakes:
Using single closing brace
Forgetting to close interpolation
Thinking square brackets are needed
5. You want to display a greeting that changes based on time using mustache syntax. Which template code correctly shows 'Good morning' if hour is less than 12, otherwise 'Good afternoon'?