0
0
Vueframework~10 mins

GET requests in components in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GET requests in components
Component starts
Setup function runs
Trigger GET request
Fetch data from server
Update reactive state
Component re-renders with data
User sees updated content
This flow shows how a Vue component triggers a GET request during setup, updates its state with the fetched data, and re-renders to show the new content.
Execution Sample
Vue
import { ref, onMounted } from 'vue';

export default {
  setup() {
    const data = ref(null);
    onMounted(async () => {
      const res = await fetch('https://api.example.com/data');
      data.value = await res.json();
    });
    return { data };
  }
};
This Vue component fetches data from an API when it mounts and stores it in a reactive variable to display.
Execution Table
StepActionState BeforeState AfterOutput/Effect
1Component setup startsdata = nulldata = nullComponent prepares reactive state
2onMounted hook registereddata = nulldata = nullReady to fetch after mount
3Component mountsdata = nulldata = nullTrigger onMounted callback
4Fetch GET request sentdata = nulldata = nullWaiting for server response
5Response receiveddata = nulldata = {json data}Data stored in reactive variable
6Component re-rendersdata = {json data}data = {json data}UI updates to show fetched data
7User sees updated contentdata = {json data}data = {json data}Data displayed on screen
💡 Data fetched and reactive state updated, component finished rendering with new data.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
datanullnull{json data}{json data}
Key Moments - 3 Insights
Why does the data variable start as null and then change?
The data variable is initially null because no data is loaded yet. After the GET request completes (see step 5 in execution_table), the data variable updates with the fetched JSON, triggering the component to re-render.
Why do we use onMounted to trigger the GET request?
Using onMounted ensures the GET request runs only after the component is fully loaded in the browser, preventing errors and ensuring the UI is ready to update with the fetched data (see step 3 and 4).
What happens if the GET request takes time to respond?
The component shows no data (null) until the response arrives. Once the data is received (step 5), the reactive state updates and the UI refreshes automatically.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'data' after step 3?
Anull
B{json data}
Cundefined
Dempty string
💡 Hint
Check the 'State After' column for step 3 in the execution_table.
At which step does the component update its UI with the fetched data?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for the step where 'Component re-renders' and UI updates happen in the execution_table.
If the GET request URL changes, which step in the execution_table would be directly affected?
AStep 6
BStep 2
CStep 4
DStep 7
💡 Hint
Step 4 is when the fetch GET request is sent; changing the URL affects this step.
Concept Snapshot
Vue GET requests in components:
- Use onMounted() to run code after component loads
- Use fetch() with await to get data
- Store data in a reactive ref variable
- Updating ref triggers component re-render
- Display data reactively in template
Full Transcript
This visual trace shows how a Vue component performs a GET request. When the component starts, it sets a reactive variable 'data' to null. The onMounted hook waits until the component is fully loaded, then sends a fetch GET request to the server. While waiting, 'data' remains null. When the response arrives, the JSON data is stored in 'data'. This update triggers Vue to re-render the component, showing the new data on screen. This process ensures data loads smoothly after the component is ready, and the UI updates automatically when data arrives.