Hone logo
Hone
Problems

Implementing Nested Routes in a Vue.js Application (TypeScript)

This challenge focuses on building a Vue.js application with nested routes. Nested routes allow you to organize your application's navigation into a hierarchical structure, making it easier to manage complex applications with multiple sections and sub-sections. Successfully implementing nested routes is crucial for creating intuitive and well-structured user interfaces.

Problem Description

You are tasked with creating a Vue.js application that demonstrates nested routes. The application should have a main layout component (AppLayout.vue) and three top-level routes: /users, /products, and /settings. The /users route should have two child routes: /users/list (displaying a list of users) and /users/profile/:id (displaying a specific user's profile, where :id is a route parameter). The /products route should have a child route /products/add (for adding new products). The /settings route should not have any child routes.

Key Requirements:

  • Route Definitions: Define all routes, including nested routes, within your router.ts file.
  • Component Mapping: Map each route to its corresponding Vue component.
  • Route Parameter Handling: Implement the /users/profile/:id route to correctly receive and display the user ID.
  • Navigation: Provide links in the AppLayout.vue component to navigate between all routes, including the nested routes.
  • Dynamic Content: For simplicity, the user list and profile components should display placeholder text indicating the route they are on. The add product component should also display placeholder text.

Expected Behavior:

  • When the application loads, it should default to the /users/list route.
  • Clicking on links in the AppLayout.vue should navigate to the corresponding routes.
  • Visiting /users/profile/123 (or any other ID) should display a profile page with the ID displayed.
  • The AppLayout.vue should remain visible regardless of the current route.

Edge Cases to Consider:

  • Ensure that the router handles invalid route parameters gracefully (e.g., if a non-numeric ID is provided to /users/profile/:id). While error handling isn't explicitly required, consider how you might approach it.
  • Think about how you would handle more complex nested routes (e.g., routes with multiple parameters).

Examples

Example 1:

Input: User navigates to /users/profile/42
Output: A component displaying "User Profile - ID: 42"
Explanation: The router correctly identifies the route, extracts the ID '42', and passes it to the UserProfile component, which displays it.

Example 2:

Input: User navigates to /products/add
Output: A component displaying "Add Product"
Explanation: The router correctly identifies the route and renders the AddProduct component.

Example 3:

Input: User navigates to /settings
Output: A component displaying "Settings"
Explanation: The router correctly identifies the route and renders the Settings component.

Constraints

  • Vue Version: Vue 3
  • TypeScript: Must be written in TypeScript.
  • Router: Vue Router 4
  • Component Structure: Use separate components for AppLayout, UserList, UserProfile, AddProduct, and Settings.
  • Route Depth: Maximum nesting depth of 2 (e.g., /users/profile/:id is the deepest).
  • Performance: The application should load and navigate quickly. No specific performance metrics are required, but avoid unnecessary re-renders.

Notes

  • Start by defining the routes in your router.ts file.
  • Use <router-link> components for navigation.
  • Consider using named routes for more maintainable navigation.
  • The placeholder content in the components is sufficient for this challenge; you don't need to implement actual data fetching or complex UI elements.
  • Focus on the correct routing configuration and navigation.
  • Remember to import and register the router in your main.ts file.
Loading editor...
typescript