What is the purpose of the <script setup> syntax in Vue 3?
It is a simpler and more concise way to write the component's script logic using the Composition API without the need to explicitly return values or define a setup() function.
Click to reveal answer
beginner
How do you declare reactive state variables inside <script setup>?
You use the ref() or reactive() functions imported from Vue to create reactive state variables directly inside the <script setup> block.
Click to reveal answer
intermediate
Can you use props directly inside <script setup> without defining them explicitly?
No, you must define props using the defineProps() helper function to declare the expected props and their types inside <script setup>.
Click to reveal answer
intermediate
How do you emit events from a component using <script setup>?
You use the defineEmits() helper function to declare the events your component can emit, then call the returned emit function to send events.
Click to reveal answer
beginner
What is one advantage of using <script setup> over the traditional setup() function?
It reduces boilerplate code by removing the need to return variables and functions explicitly, making the component code cleaner and easier to read.
Click to reveal answer
Which function is used to declare props inside <script setup>?
AuseProps()
Bprops()
CdefineProps()
DdeclareProps()
✗ Incorrect
The defineProps() function is the official way to declare props in <script setup>.
How do you create a reactive variable inside <script setup>?
AUsing reactiveState()
BUsing reactiveVar()
CUsing useState()
DUsing ref() or reactive()
✗ Incorrect
Vue provides ref() and reactive() to create reactive variables.
What must you do to emit an event from a component using <script setup>?
AEvents cannot be emitted in <code><script setup></code>
BUse defineEmits() and call the emit function
CUse emitEvent() helper
DCall emit() directly without declaration
✗ Incorrect
You declare events with defineEmits() and then call the returned emit function.
Which of these is NOT a benefit of <script setup>?
ASupports class-based components
BAutomatic return of variables
CLess boilerplate code
DSimpler syntax for Composition API
✗ Incorrect
Vue 3 Composition API and <script setup> do not support class-based components.
Where do you write the <script setup> block in a Vue component?
AInside the <code><script></code> tag with the setup attribute
BInside the <code><template></code> tag
CInside the <code><style></code> tag
DInside a separate JavaScript file only
✗ Incorrect
You write <script setup> as a <script> tag with the setup attribute.
Explain how to define props and emit events using <script setup> in Vue 3.
Think about how you tell Vue what props and events your component uses.
You got /3 concepts.
Describe the main advantages of using <script setup> compared to the traditional setup function.
Focus on how it makes writing components easier and cleaner.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using <script setup> in a Vue component?
easy
A. It requires class-based components instead of functions.
B. It automatically adds CSS styles to the component.
C. It disables the use of reactive variables.
D. It removes the need for export default and simplifies code.
Solution
Step 1: Understand the purpose of <script setup>
This syntax is designed to simplify Vue components by removing boilerplate code like export default.
Step 2: Compare options with this purpose
Only It removes the need for export default and simplifies code. correctly states that it removes the need for export default and simplifies code.
Final Answer:
It removes the need for export default and simplifies code. -> Option D
Step 1: Understand how ref works in template interpolation
In Vue, a ref is an object with a value property. To display the inner value, you must use message.value in script or just message in template because Vue unwraps refs in templates.
Step 2: Check the template interpolation
The template uses {{ message }}. Vue unwraps refs here, so it should display the string inside message.value, which is 'Hi'.
Step 3: Identify the actual behavior
In Vue 3 with <script setup>, refs are unwrapped automatically in templates, so it should display 'Hi'. The code sets message.value = 'Hi' correctly, so the output is 'Hi'.
Final Answer:
<p>Hi</p> -> Option C
Quick Check:
Template unwraps ref = Hi [OK]
Hint: Remember Vue unwraps refs automatically in templates [OK]
Common Mistakes:
Thinking you must write message.value in template
Confusing ref object with its value
Assuming message is undefined
4. Identify the error in the following <script setup> code snippet:
<script setup>
import { reactive } from 'vue'
const state = reactive({ count: 0 })
function increment() {
state.count++
}
</script>
<template>
<button @click="increment()">Increment</button>
<p>Count: {{ state.count }}</p>
</template>
medium
A. There is no error; the code works correctly.
B. The increment function is not exposed to the template and cannot be called.
C. The reactive object should be declared with ref() instead of reactive().
D. The @click event syntax is incorrect; it should be v-on:click.
Solution
Step 1: Analyze the reactive state and function
The code uses reactive to create a reactive object state with a count property. The increment function modifies state.count.
Step 2: Check template usage and event binding
The template uses @click="increment()" which is valid shorthand for v-on:click. The function increment is defined in <script setup> and is automatically exposed to the template.
Final Answer:
There is no error; the code works correctly. -> Option A
Quick Check:
Script setup exposes functions automatically = C [OK]
Hint: Functions in script setup are auto-exposed to template [OK]
Common Mistakes:
Thinking reactive must be replaced by ref for objects
Believing functions need explicit export
Confusing @click with invalid syntax
5. You want to create a Vue component using <script setup> that fetches data asynchronously when the component mounts and stores it in a reactive variable. Which is the correct way to do this?
hard
A.
<script setup>
import { ref, onMounted } from 'vue'
const data = ref(null)
onMounted(async () => {
const res = await fetch('https://api.example.com/data')
data.value = await res.json()
})
</script>
B.
<script setup>
import { reactive } from 'vue'
const data = reactive(null)
async function fetchData() {
const res = await fetch('https://api.example.com/data')
data = await res.json()
}
fetchData()
</script>
C.
<script setup>
import { ref } from 'vue'
const data = ref(null)
fetch('https://api.example.com/data').then(res => {
data = res.json()
})
</script>