Fetching and Displaying Asynchronous Data in a Vue Component
This challenge focuses on implementing asynchronous data fetching within a Vue.js component using TypeScript. You'll need to manage loading states, potential errors, and display the fetched data effectively, simulating a common real-world scenario.
Problem Description
Your task is to create a Vue.js component that fetches data from a mock API endpoint asynchronously. This component should gracefully handle the different states of the asynchronous operation: loading, success, and error. The fetched data, which represents a list of users, should be displayed in a user-friendly manner.
Key Requirements:
- Asynchronous Data Fetching: Implement a function to fetch data (e.g., from a mock API). This function should simulate network latency.
- State Management: Manage three distinct states for the data fetching process:
loading: While the data is being fetched.data: Once the data has been successfully fetched.error: If an error occurs during fetching.
- Display Logic: Conditionally render UI elements based on the current state:
- Show a "Loading..." message when
loadingis true. - Display the fetched user list when
datais available anderroris null. - Show an error message when
erroris present.
- Show a "Loading..." message when
- TypeScript Integration: Utilize TypeScript for strong typing of component data, props (if any), and the fetched data.
Expected Behavior:
When the component mounts, it should immediately initiate the data fetching process. Initially, a loading indicator will be displayed. If the fetch is successful, the list of users will be rendered. If an error occurs, an informative error message will be shown.
Edge Cases:
- Network Error: Simulate a network error during the data fetch.
- Empty Data: Handle cases where the API returns an empty list of users.
Examples
Example 1: Successful Data Fetch
- Input: (Component mounts and mock API successfully returns user data)
- Output:
<div> <h2>User List</h2> <ul> <li>John Doe</li> <li>Jane Smith</li> <li>Peter Jones</li> </ul> </div> - Explanation: The component fetches user data, and upon successful retrieval, displays the names of the users in an unordered list.
Example 2: Loading State
- Input: (Component mounts, data fetching is in progress)
- Output:
<div> <p>Loading users...</p> </div> - Explanation: Before the data is ready or if an error occurs, a loading message is displayed.
Example 3: Error State
- Input: (Component mounts, mock API simulates a network error)
- Output:
<div> <p>Error fetching users: Network Error</p> </div> - Explanation: If an error occurs during data fetching (e.g., a simulated network issue), an error message is displayed to the user.
Constraints
- The mock API simulation should introduce a random delay between 500ms and 2000ms.
- The mock API should have a 10% chance of returning an error.
- The data structure for a single user will be
{ id: number; name: string; }. - The fetched data will be an array of these user objects.
Notes
- Consider using Vue's Composition API (
setupfunction) for managing reactive state and lifecycle hooks. - A common pattern for handling asynchronous operations in Vue is to use
reffor reactive variables andonMountedfor initiating the fetch. - You'll need to create a mock API function that returns a
Promiseto simulate asynchronous behavior and allow for error simulation. - Think about how to type the fetched data to ensure type safety throughout your component.