Dynamic Route Parameters in Vue.js with TypeScript
This challenge focuses on implementing dynamic route parameters in a Vue.js application using Vue Router and TypeScript. Route parameters allow you to capture segments of a URL and use them to display dynamic content, such as individual product pages or user profiles. Mastering this concept is crucial for building single-page applications with complex navigation.
Problem Description
You are tasked with creating a Vue.js component that displays details based on a route parameter. The application should have a route defined to handle URLs like /products/:id, where :id is a dynamic segment representing a product ID. Your component should:
- Access the
idparameter: Retrieve the value of theidparameter from the current route. - Fetch Product Data: Simulate fetching product data based on the
idfrom a mock API (you don't need to implement a real API). For simplicity, assume the mock API returns a product object with anidand anameproperty. If theidis not found, display an error message. - Display Product Details: Render the product's
idandnamewithin the component. - Handle Invalid IDs: If the provided
iddoesn't correspond to a product in the mock data, display an appropriate error message.
Key Requirements:
- Use Vue Router for navigation and route parameter handling.
- Implement the component in TypeScript.
- Use
useRoutefromvue-routerto access route information. - Simulate an asynchronous data fetch.
- Provide clear error handling.
Expected Behavior:
- Navigating to
/products/123should display the product with ID 123 and its name. - Navigating to
/products/456should display the product with ID 456 and its name. - Navigating to
/products/789(assuming this ID doesn't exist in the mock data) should display an error message. - Navigating to
/products/(without an ID) should display an error message.
Edge Cases to Consider:
- What happens if the route parameter is not a number? (For this challenge, assume it will always be a number, but consider it for future development).
- What happens if the route parameter is an empty string?
- What happens if the route parameter is missing entirely?
Examples
Example 1:
Input: Route: /products/123, Mock Data: [{ id: 123, name: "Awesome Widget" }, { id: 456, name: "Super Gadget" }]
Output: Displays: "ID: 123, Name: Awesome Widget"
Explanation: The component successfully retrieves and displays the product with ID 123.
Example 2:
Input: Route: /products/789, Mock Data: [{ id: 123, name: "Awesome Widget" }, { id: 456, name: "Super Gadget" }]
Output: Displays: "Error: Product not found"
Explanation: The component correctly handles the case where the product ID is not found in the mock data.
Example 3:
Input: Route: /products/, Mock Data: [{ id: 123, name: "Awesome Widget" }, { id: 456, name: "Super Gadget" }]
Output: Displays: "Error: Product ID is required"
Explanation: The component handles the case where the route parameter is missing.
Constraints
- Data Fetching: Simulate data fetching with a
setTimeoutfunction to mimic an asynchronous API call (e.g.,setTimeout(() => { ... }, 500);). - Mock Data: Use a hardcoded array of product objects as mock data. The array should contain at least two products with different IDs.
- Component Structure: The solution should be a single Vue component.
- Error Handling: Provide user-friendly error messages.
- TypeScript: The code must be written in TypeScript and adhere to good TypeScript practices.
Notes
- Consider using
watchto react to changes in the route parameter. However,useRouteis generally preferred for accessing route information within components. - Think about how to structure your component to separate the data fetching logic from the rendering logic.
- Focus on clarity and readability in your code. Use meaningful variable names and comments where necessary.
- Remember to install
vue-routerand configure it in yourmain.tsfile. You'll need to create a route that matches/products/:id.