0
0
Vueframework~10 mins

Setup function basics in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Setup function basics
Component starts
Call setup()
Declare reactive state
Return state and methods
Template uses returned values
Component renders with reactive data
The setup function runs first in a Vue component, sets up reactive state and methods, then returns them for the template to use.
Execution Sample
Vue
import { ref } from 'vue'
export default {
  setup() {
    const count = ref(0)
    function increment() { count.value++ }
    return { count, increment }
  }
}
This code creates a reactive count variable and an increment function inside setup, then returns them for the template.
Execution Table
StepActionEvaluationResult
1Call setup()Runs setup functionSetup function starts
2Declare count = ref(0)Creates reactive count with value 0count.value = 0
3Define increment()Function increments count.valueincrement() ready
4Return { count, increment }Expose reactive state and methodReturned object with count and increment
5Template uses countReads count.valueDisplays 0 initially
6Call increment()count.value++count.value becomes 1
7Template re-rendersReflects updated count.valueDisplays 1
💡 Setup function completes after returning reactive state and methods
Variable Tracker
VariableStartAfter Step 2After Step 6Final
count.valueundefined011
Key Moments - 3 Insights
Why do we use ref() for count inside setup?
ref() makes count reactive so Vue tracks changes and updates the template automatically, as shown in execution_table step 2 and 7.
What happens if we don't return count and increment from setup?
The template cannot access them, so no reactive updates happen. This is why returning them in step 4 is essential.
Why do we access count.value instead of count directly?
ref() wraps the value inside an object; .value accesses the actual data, as seen in steps 2 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is count.value after step 2?
Aundefined
B0
C1
Dnull
💡 Hint
Check the 'Result' column at step 2 in the execution_table.
At which step does the template first display the updated count value?
AStep 7
BStep 6
CStep 5
DStep 4
💡 Hint
Look for when the template re-renders with the new count in execution_table step 7.
If we forget to return increment from setup, what happens?
ATemplate can still call increment
Bcount stops being reactive
Cincrement is not accessible in template
DSetup function errors out
💡 Hint
Refer to key_moments about returning values from setup and execution_table step 4.
Concept Snapshot
Setup function runs first in Vue component.
Declare reactive state with ref() or reactive().
Define methods inside setup.
Return state and methods for template use.
Template accesses returned values reactively.
Use .value to access ref() data.
Full Transcript
In Vue, the setup function is the first code that runs when a component starts. Inside setup, you create reactive variables using ref() or reactive(). You can also define functions to change these variables. After setting up, you return the reactive variables and functions so the template can use them. The template then shows the current values and updates automatically when the reactive data changes. For example, a count variable created with ref(0) starts at zero. When you call an increment function that adds one to count.value, the template updates to show the new count. Remember, ref() wraps the value, so you use .value to get or set the actual data. If you forget to return a variable or function from setup, the template cannot access it and won't update. This flow helps Vue components stay reactive and easy to manage.