Routing helps single-page apps (SPAs) show different pages without reloading the whole site. It makes the app feel fast and smooth, like a real website with many pages.
Why routing is needed for SPAs in Vue
Introduction
Syntax
Vue
import { createRouter, createWebHistory } from 'vue-router'; const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ]; const router = createRouter({ history: createWebHistory(), routes }); export default router;
Routes connect URLs to components that show the right content.
createWebHistory() uses normal URLs without hashes (#).
Examples
Vue
const routes = [ { path: '/', component: Home }, { path: '/profile', component: Profile } ];
Vue
const routes = [ { path: '/user/:id', component: User } ];
Vue
const routes = [ { path: '/settings', component: Settings, meta: { requiresAuth: true } } ];
Sample Program
This Vue app uses routing to switch between Home and About pages without reloading. The links update the URL and the displayed content changes smoothly.
Vue
import { createApp } from 'vue'; import { createRouter, createWebHistory } from 'vue-router'; const Home = { template: '<h2>Home Page</h2>' }; const About = { template: '<h2>About Page</h2>' }; const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ]; const router = createRouter({ history: createWebHistory(), routes }); const app = createApp({ template: ` <div> <nav> <router-link to="/">Home</router-link> | <router-link to="/about">About</router-link> </nav> <router-view></router-view> </div> ` }); app.use(router); app.mount('#app');
Important Notes
Routing keeps the app fast by avoiding full page reloads.
URLs change so users can bookmark or share specific views.
Vue Router is the official routing library for Vue apps.
Summary
Routing lets SPAs show different pages without reloading.
It changes the URL so users can share or bookmark views.
Vue Router connects URLs to components to display the right content.
Practice
1. Why do Single Page Applications (SPAs) need routing?
Routing in SPAs helps to:easy
Solution
Step 1: Understand SPA behavior
SPAs load a single HTML page and dynamically update content without full reloads.Step 2: Role of routing in SPAs
Routing allows switching views inside the SPA without refreshing the browser, improving speed and user experience.Final Answer:
Change views without reloading the whole page -> Option AQuick Check:
Routing in SPAs = Change views without reload [OK]
Hint: Routing changes views inside SPA without page reload [OK]
Common Mistakes:
- Thinking SPAs reload the whole page on navigation
- Confusing routing with server-side page loads
- Believing routing disables navigation
2. Which of the following is the correct way to define a route in Vue Router?
easy
Solution
Step 1: Recall Vue Router route syntax
Routes are defined as an array of objects withpathandcomponentkeys.Step 2: Match syntax with options
const routes = [{ path: '/home', component: HomePage }] correctly uses an array with an object containingpathandcomponent.Final Answer:
const routes = [{ path: '/home', component: HomePage }] -> Option BQuick Check:
Vue Router routes = array of objects with path and component [OK]
Hint: Routes are array of objects with path and component keys [OK]
Common Mistakes:
- Using object instead of array for routes
- Missing curly braces for route objects
- Incorrect array structure without keys
3. Given this Vue Router setup:
What component will Vue display?
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
];
// User visits '/about'What component will Vue display?
medium
Solution
Step 1: Check the route matching the URL
The URL '/about' matches the route with path '/about' which uses the About component.Step 2: Determine displayed component
Vue Router will render the About component for the '/about' path.Final Answer:
About component -> Option CQuick Check:
URL '/about' shows About component [OK]
Hint: URL path matches route path to show component [OK]
Common Mistakes:
- Assuming default '/' route always shows
- Confusing component names
- Thinking Vue reloads page instead of switching component
4. What is wrong with this Vue Router code?
const routes = [
{ path: '/home', component: Home },
{ path: '/about', component: About }
]
const router = createRouter({
history: createWebHistory,
routes
})medium
Solution
Step 1: Check usage of createWebHistory
createWebHistory is a function and must be called with parentheses to return history object.Step 2: Identify error in code
Code usescreateWebHistorywithout parentheses, so history is not set correctly.Final Answer:
createWebHistory is missing parentheses -> Option AQuick Check:
Functions need () when called [OK]
Hint: Functions like createWebHistory need () to run [OK]
Common Mistakes:
- Forgetting parentheses on function calls
- Confusing array and object syntax for routes
- Thinking component names must be strings
5. You want users to bookmark and share specific views in your Vue SPA. How does routing help achieve this?
hard
Solution
Step 1: Understand URL role in SPAs
SPAs use routing to change the URL path dynamically without full page reloads.Step 2: How routing supports bookmarking
By updating the URL, routing lets users bookmark or share links that open the app at the same view.Final Answer:
Routing updates the URL to reflect the current view without reloading -> Option DQuick Check:
Routing updates URL for sharing/bookmarking [OK]
Hint: Routing changes URL so views can be bookmarked/shared [OK]
Common Mistakes:
- Thinking routing reloads the page for bookmarks
- Believing URLs stay static in SPAs
- Assuming routing disables URL changes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
