0
0
Vueframework~20 mins

Script setup syntax in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Script Setup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Vue component using script setup?

Consider this Vue 3 component using <script setup> syntax. What will it display on the page?

Vue
<template>
  <p>{{ message }}</p>
</template>

<script setup>
import { ref } from 'vue'
const message = ref('Hello from script setup!')
</script>
AAn empty paragraph
BHello from script setup!
CError: message is not defined
Dundefined
Attempts:
2 left
💡 Hint

Remember that ref creates a reactive variable and you can use it directly in the template.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a reactive object using script setup?

Choose the correct way to define a reactive object named user with properties name and age inside <script setup>.

Aconst user = reactive({ name: 'Alice', age: 30 })
Bconst user = ref({ name: 'Alice', age: 30 })
Cconst user = reactive('name: Alice, age: 30')
Dconst user = ref('name: Alice, age: 30')
Attempts:
2 left
💡 Hint

Use reactive for objects and ref for primitive values or single objects.

🔧 Debug
advanced
2:00remaining
Why doesn't this script setup component update the count?

Look at this component code. Why doesn't the displayed count update?

Vue
<template>
  <p>{{ count }}</p>
</template>

<script setup>
let count = 0
setInterval(() => {
  count++
}, 1000)
</script>
ABecause <code>count</code> is not reactive, so Vue cannot track changes
BBecause <code>setInterval</code> is not allowed inside <code>script setup</code>
CBecause <code>count</code> should be declared with <code>const</code> instead of <code>let</code>
DBecause the template cannot access variables declared in <code>script setup</code>
Attempts:
2 left
💡 Hint

Think about how Vue tracks changes to update the template.

lifecycle
advanced
2:00remaining
How to correctly use the onMounted lifecycle hook in script setup?

Which code snippet correctly runs a function when the component is mounted using <script setup>?

A
import { onMounted } from 'vue'
setup() { onMounted(() =&gt; { console.log('Mounted!') }) }
BonMounted(() => { console.log('Mounted!') })
C
import { onMounted } from 'vue'
onMounted(() =&gt; { console.log('Mounted!') })
D
import { mounted } from 'vue'
mounted(() =&gt; { console.log('Mounted!') })
Attempts:
2 left
💡 Hint

Remember script setup is a simpler syntax without explicit setup() function.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using <script setup> over traditional setup() function in Vue 3?

Choose the best explanation for why <script setup> is preferred in many Vue 3 projects.

AIt automatically converts all variables to reactive without importing anything
BIt disables the template syntax and requires manual DOM manipulation
CIt allows using class-based components inside Vue 3 without extra setup
DIt reduces boilerplate by removing the need to return variables and allows direct use of composition API features
Attempts:
2 left
💡 Hint

Think about how script setup simplifies component code.