Hone logo
Hone
Problems

Implementing a Basic Router-View Component in Vue (TypeScript)

This challenge asks you to implement a simplified version of Vue Router's <router-view> component. The <router-view> component is crucial for displaying different components based on the current route. This exercise will help you understand how Vue's reactivity system and component rendering work together to manage dynamic content.

Problem Description

You need to create a Vue component called RouterView that dynamically renders a component based on a currentRoute prop. The currentRoute prop will be an object with a component property, which holds the component to be rendered. The RouterView component should render this component within its own template. If the currentRoute prop is null or undefined, the component should render nothing (or a placeholder message like "Loading...").

Key Requirements:

  • Dynamic Rendering: The component must render a different component based on the value of the currentRoute.component prop.
  • Reactivity: The component should re-render whenever the currentRoute prop changes.
  • Null/Undefined Handling: Gracefully handle cases where currentRoute is null or undefined.
  • TypeScript: The component must be written in TypeScript.

Expected Behavior:

  1. When a new currentRoute is passed to the RouterView component, the component should render the component specified by currentRoute.component.
  2. If currentRoute is null or undefined, the component should render nothing (or a placeholder).
  3. Changes to the rendered component's props should not affect the RouterView component itself.

Edge Cases to Consider:

  • What happens if currentRoute.component is not a valid Vue component? (For simplicity, you can assume it is a valid component in this challenge).
  • How to handle potential errors during component rendering? (For simplicity, you can assume no errors occur).

Examples

Example 1:

Input: currentRoute = { component: { template: '<div>Hello from Component A!</div>' } }
Output: <div>Hello from Component A!</div>
Explanation: The RouterView renders the component specified by currentRoute.component.  In this case, it renders a simple component with a template.

Example 2:

Input: currentRoute = null
Output: (Nothing rendered - or a placeholder like "Loading...")
Explanation: The RouterView handles the null case and doesn't render anything.

Example 3:

Input: currentRoute = { component: { template: '<div>Hello from Component B!</div>' } } (after previously being null)
Output: <div>Hello from Component B!</div>
Explanation: The RouterView dynamically updates its content when the currentRoute changes.

Constraints

  • The component must be written in TypeScript.
  • The component should be a functional component.
  • The component should only rely on the currentRoute prop. No external state management is required.
  • The component should be relatively simple and focused on demonstrating the core concept of dynamic rendering.

Notes

  • Think about how to use Vue's reactivity system to detect changes in the currentRoute prop.
  • Consider using renderFunction to dynamically render the component.
  • This is a simplified implementation and doesn't include all the features of Vue Router's <router-view>. The goal is to understand the fundamental principles.
  • You can use a simple component definition for currentRoute.component for testing purposes (e.g., a component with just a template).
Loading editor...
typescript