Hone logo
Hone
Problems

Dynamic Route Configuration in Vue 3 with TypeScript

This challenge focuses on implementing a flexible and dynamic route configuration system in a Vue 3 application using TypeScript. Instead of hardcoding routes, you'll build a component that reads route definitions from a data source (a simple JSON object in this case) and generates the necessary Vue Router configuration. This is useful for applications where routes are frequently updated or managed externally.

Problem Description

You need to create a Vue component called RouteConfigurator that takes a JSON object representing route definitions as a prop. This JSON object will contain an array of route objects. Each route object should have the following properties:

  • path: (string) The route path (e.g., /about, /users/:id).
  • name: (string) The route name (e.g., AboutPage, UserDetail).
  • component: (string) The name of the Vue component to render for this route (e.g., About, UserDetailComponent). Assume these components are already registered globally in your Vue application.
  • meta: (object, optional) A metadata object for the route. This can contain any additional information you want to associate with the route (e.g., requiresAuth: true).

The RouteConfigurator component should dynamically generate the Vue Router configuration based on the provided route definitions and return it. The component should not render anything directly to the DOM; its sole purpose is to generate the router configuration.

Key Requirements:

  • The component must be written in TypeScript.
  • It must accept a routes prop of type RouteDefinition[].
  • It must return a createRouter function (from vue-router) that, when called, creates a Vue Router instance with the dynamically generated routes.
  • The returned router instance should be configured with history mode set to history.

Expected Behavior:

When the RouteConfigurator component is mounted, it should process the routes prop and return a function that creates a Vue Router instance with the configured routes. The returned router instance should be usable in your Vue application.

Edge Cases to Consider:

  • Empty routes array: The router should be created with an empty array of routes.
  • Invalid route definitions: Handle cases where a route definition is missing a required property (e.g., path, name, or component). Log an error to the console for invalid routes, but continue processing the remaining routes.
  • Component names: Ensure the component names provided in the component property are valid and exist within your Vue application. (For simplicity, assume they always exist in this challenge).

Examples

Example 1:

Input:
routes: [
  { path: '/about', name: 'AboutPage', component: 'About' },
  { path: '/users/:id', name: 'UserDetail', component: 'UserDetailComponent' }
]
Output:
A function that, when called, returns a Vue Router instance with the above routes configured.
Explanation: The component processes the input array and generates a router instance with two routes: one for `/about` and one for `/users/:id`.

Example 2:

Input:
routes: []
Output:
A function that, when called, returns a Vue Router instance with an empty array of routes.
Explanation: The component handles the edge case of an empty routes array gracefully.

Example 3:

Input:
routes: [
  { path: '/home', name: 'HomePage', component: 'Home' },
  { path: '/contact', name: 'ContactPage', component: 'Contact' },
  { path: '/invalid', name: 'InvalidRoute', component: 'NonExistentComponent' } // Invalid component
]
Output:
A function that, when called, returns a Vue Router instance with the first two routes configured. An error message is logged to the console regarding the invalid component 'NonExistentComponent'.
Explanation: The component processes the valid routes and logs an error for the invalid route, continuing with the valid routes.

Constraints

  • The routes prop must be an array of objects.
  • Each object in the routes array must have path, name, and component properties.
  • The component names must be strings.
  • The solution must use Vue Router 4 and Vue 3.
  • The solution must be written in TypeScript.
  • Performance is not a primary concern for this challenge; focus on correctness and clarity.

Notes

  • You'll need to import createRouter from vue-router.
  • Consider using a functional component for simplicity.
  • The RouteDefinition type should be defined to ensure type safety.
  • Error handling should be done via console.error for invalid route definitions.
  • This challenge focuses on the generation of the router configuration, not its integration into a larger application. You don't need to create a full Vue application for this. Just the RouteConfigurator component.
Loading editor...
typescript