Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Lifecycle and Data Requests

Lifecycle and Data Requests

In this chapter you will learn Vue 3's lifecycle hooks and use fetch at the right moment to load blog data from a JSON file.


What Is the Lifecycle?

Every Vue component goes through a series of stages from creation to destruction; these stages are called the "lifecycle".

Vue provides corresponding hook functions at each stage, letting you execute code at specific points in time.

For example: if you want to request data after the component finishes loading, you need to use onMounted this hook.


Vue 3 Lifecycle Hooks Overview

Hook Function Execution Timing Common Scenarios
setup() Component initialization; executed earliest Define reactive data, computed properties
onBeforeMount Before the component is mounted to the DOM Less commonly used
onMounted After the component is mounted to the DOM Initiate network requests, initialize third-party libraries, obtain DOM references
onBeforeUpdate Before data changes and DOM updates Less commonly used
onUpdated After data changes and DOM updates Operations after DOM updates
onBeforeUnmount Before the component is destroyed Clear timers, cancel subscriptions, remove event listeners
onUnmounted After the component is destroyed Confirmation of Cleanup Work

For the blog project, the hook we use most is onMounted — it fetches article data after the page loads.

Lifecycle hooks must be written inside

A component can call onMounted multiple times, and they execute in registration order.


Loading JSON Data with fetch

We will put the article data into a JSON file, then load it with fetch inside onMounted.

Step 1: Create the Data File

Example

// File path: public/posts.json

[

  {

    "id": 1,

    "title": "Complete Guide to Getting Started with Vue 3",

    "summary": "Learn the Vue 3 Composition API from scratch, covering core concepts such as ref, reactive, and computed.",

    "content": "

Why Learn Vue 3?

Vue 3 is one of the most popular frontend frameworks today...

",

    "category": "Vue",

    "date": "2024-05-10"

  },

  {

    "id": 2,

    "title": "JavaScript Asynchronous Programming Explained",

    "summary": "Understand Promises, async/await, the event loop, and the microtask queue in one article.",

    "content": "

What Is Asynchronous?

JS is single-threaded...

",

    "category": "JavaScript",

    "date": "2024-05-08"

  },

  {

    "id": 3,

    "title": "CSS Grid Layout in Practice",

    "summary": "Easily implement complex responsive layouts with CSS Grid.",

    "content": "

Getting Started with Grid

Grid is a two-dimensional layout system...

",

    "category": "CSS",

    "date": "2024-05-05"

  }

]

Place the JSON file in the public/ directory, and Vite will serve it directly as a static file. In the browser you can access it directly via /posts.json. Files in public are not compiled; they are copied as-is into the build output.

Step 2: Fetch Data in the Component

Example


Handling the Three States of Async Requests

Any data request should cover the following three UI states; otherwise the user experience will be poor:

State When It Appears UI Presentation
Loading After the request is sent, before the response returns Loading animation, skeleton screen
Success Data returned correctly Render content normally
Failure Network error, server exception Error message + retry button

You can also handle a fourth state — empty data (request succeeded but returned an empty array), giving the user a clear prompt instead of a blank page.


Hands-On: Switch the Detail Page to Also Load via fetch

The detail page needs to find the corresponding article from the JSON based on the ID in the URL.

You can reuse the data-loading logic — call fetch on the detail page too, then filter.

Example


Chapter Summary

In this chapter you mastered the most core lifecycle hook onMounted, loading JSON data from the public directory with fetch, and how to handle the four UI states of requests (loading/success/failure/empty).

Now the blog data is loaded asynchronously from an external JSON file, closer to how real projects work.