Vue Router Setup for a Multi-Page Application
This challenge will guide you through setting up Vue Router in a Vue.js 3 TypeScript project. Properly configuring Vue Router is essential for building single-page applications (SPAs) with multiple views and navigation capabilities, providing a seamless user experience.
Problem Description
Your task is to create a robust Vue Router setup for a basic multi-page application. This involves defining routes, creating corresponding Vue components for each route, and configuring the router to manage navigation between these components.
Key Requirements:
- Define Routes: Create routes for at least three distinct pages:
- A Home page.
- An About page.
- A Contact page.
- Create Components: Develop simple Vue components (written in TypeScript) for each of the defined routes. These components can be very basic, perhaps just displaying a heading like "Welcome to Home Page".
- Configure Router:
- Instantiate
createRouterfromvue-router. - Use the
historymode (e.g.,createWebHistory). - Define the
routesarray, mapping paths to components. - Include a "Not Found" route (e.g.,
/*) that redirects to a 404 page.
- Instantiate
- Integrate Router:
- Import and install the configured router into your main Vue application (
main.ts). - Use the
<router-view>component in your rootApp.vueto render the matched route components. - Add navigation links (using
<router-link>) inApp.vueto allow users to navigate between the Home, About, and Contact pages.
- Import and install the configured router into your main Vue application (
Expected Behavior:
- When the application loads, the Home page should be displayed.
- Clicking on a navigation link (e.g., "About") should change the URL in the browser and render the corresponding component within
<router-view>. - Navigating to a non-existent path should display a "Page Not Found" message.
Edge Cases:
- Handling dynamic routes (though not strictly required for this basic setup, consider how you might approach it for future expansion).
- Ensuring type safety for route definitions and component props if applicable.
Examples
Example 1: Basic Navigation
-
Input: A Vue.js 3 TypeScript project with the following file structure:
src/router/index.ts(to be created)src/views/Home.vue(simple component)src/views/About.vue(simple component)src/views/Contact.vue(simple component)src/App.vue(root component with<router-view>and links)src/main.ts(application entry point)
-
Output:
- Navigating to
/displays theHome.vuecomponent. - Navigating to
/aboutdisplays theAbout.vuecomponent. - Navigating to
/contactdisplays theContact.vuecomponent.
- Navigating to
-
Explanation: The router configuration in
src/router/index.tscorrectly maps these paths to their respective components, andApp.vue's<router-link>elements facilitate the transitions.
Example 2: Not Found Route
-
Input: The same setup as Example 1, but the user navigates to
/nonexistent-page. -
Output: A component (e.g.,
NotFound.vue- you can create a simple one) is rendered, displaying a "404 - Page Not Found" message. -
Explanation: The catch-all route (
/*) configured insrc/router/index.tsintercepts any path that doesn't match other defined routes and renders the designated 404 component.
Constraints
- The project must use Vue.js 3 and TypeScript.
- Vue Router version 4 or later must be used.
- The
createWebHistorymode should be used for routing. - All route definitions and component types should be as type-safe as possible using TypeScript.
- The solution should be a clear and functional implementation of Vue Router.
Notes
- You'll need to install
vue-routeras a dependency in your project:npm install vue-router@nextoryarn add vue-router@next. - Consider creating a separate
NotFound.vuecomponent for the 404 route for better organization. - Think about how you would structure your
routesarray for maintainability as the application grows. - The
main.tsfile is where you'll typically create your Vue app instance and use theuse()method to register the router plugin. - For the components, you can use the Composition API (
<script setup lang="ts">) or the Options API.