The script setup syntax in Vue makes writing components simpler and cleaner by reducing extra code and boilerplate.
Script setup syntax in Vue
<script setup> import { ref } from 'vue' // Your component logic here const count = ref(0) function increment() { count.value++ } </script>
export default in script setup.<script setup> import { ref } from 'vue' const message = ref('Hello!') </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>
script setup for type safety.<script setup lang="ts"> import { ref, type Ref } from 'vue' const name: Ref<string> = ref('Vue') </script> <template> <h1>Hello, {{ name }}</h1> </template>
This Vue component uses script setup to create a simple button that counts clicks. The count variable is reactive and updates the displayed number each time the button is clicked. The button includes an accessible label for screen readers.
<script setup> import { ref } from 'vue' const count = ref(0) function increment() { count.value++ } </script> <template> <button @click="increment" aria-label="Increment counter"> Clicked {{ count }} times </button> </template>
All top-level variables and functions in script setup are automatically exposed to the template.
You can import other modules normally inside script setup.
Use lang="ts" attribute for TypeScript support.
Script setup simplifies Vue component code by removing the need for export default.
Reactive variables and functions are directly usable in the template.
It supports TypeScript and improves readability for Composition API usage.
Practice
<script setup> in a Vue component?Solution
Step 1: Understand the purpose of
This syntax is designed to simplify Vue components by removing boilerplate code like<script setup>export default.Step 2: Compare options with this purpose
Only It removes the need forexport defaultand simplifies code. correctly states that it removes the need forexport defaultand simplifies code.Final Answer:
It removes the need for export default and simplifies code. -> Option DQuick Check:
Script setup simplifies code = A [OK]
- Thinking script setup adds styles automatically
- Believing reactive variables are disabled
- Confusing script setup with class-based components
<script setup>?Solution
Step 1: Recall Vue 3 Composition API reactive variable syntax
Reactive variables useref()to wrap primitive values like numbers.Step 2: Check each option
const count = ref(0); usesref(0), which is correct. Options A, C, and D use incorrect or non-existent functions.Final Answer:
const count = ref(0); -> Option BQuick Check:
Use ref() for reactive primitives = B [OK]
- Using reactive() for primitive values
- Using undefined functions like reactiveRef or reactiveVar
- Using var instead of const or let
<script setup> code, what will be rendered inside the template?<script setup>
import { ref } from 'vue'
const message = ref('Hello')
message.value = 'Hi'
</script>
<template>
<p>{{ message }}</p>
</template>Solution
Step 1: Understand how ref works in template interpolation
In Vue, a ref is an object with avalueproperty. To display the inner value, you must usemessage.valuein script or justmessagein 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 insidemessage.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 setsmessage.value = 'Hi'correctly, so the output is 'Hi'.Final Answer:
<p>Hi</p> -> Option CQuick Check:
Template unwraps ref = Hi [OK]
- Thinking you must write message.value in template
- Confusing ref object with its value
- Assuming message is undefined
<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>Solution
Step 1: Analyze the reactive state and function
The code usesreactiveto create a reactive objectstatewith acountproperty. Theincrementfunction modifiesstate.count.Step 2: Check template usage and event binding
The template uses@click="increment()"which is valid shorthand forv-on:click. The functionincrementis defined in<script setup>and is automatically exposed to the template.Final Answer:
There is no error; the code works correctly. -> Option AQuick Check:
Script setup exposes functions automatically = C [OK]
- Thinking reactive must be replaced by ref for objects
- Believing functions need explicit export
- Confusing @click with invalid syntax
<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?Solution
Step 1: Understand how to fetch data on component mount
Use theonMountedlifecycle hook to run code after the component is ready.Step 2: Check reactive variable usage and async fetch
Useref(null)for reactive data. InsideonMounted, use async/await to fetch and assign data todata.value.Step 3: Evaluate each option
<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>correctly usesref,onMounted, and async/await with proper assignment todata.value.<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>incorrectly tries to assign to a reactive object directly and calls fetchData immediately without lifecycle hook.<script setup> import { ref } from 'vue' const data = ref(null) fetch('https://api.example.com/data').then(res => { data = res.json() }) </script>assignsdatainstead ofdata.valueand lacks lifecycle hook.<script setup> import { ref, onMounted } from 'vue' const data = ref(null) onMounted(() => { fetch('https://api.example.com/data').then(res => res.json()).then(json => { data.value = json }) }) </script>uses promises correctly but<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>is cleaner and preferred with async/await.Final Answer:
Option A code snippet -> Option AQuick Check:
Use onMounted + ref + async/await = A [OK]
- Assigning directly to reactive instead of .value
- Not using lifecycle hooks for async fetch
- Mixing reactive and ref incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
