Discover how Vue Router makes multi-page apps feel effortless and smooth!
Why Vue Router installation and setup? - Purpose & Use Cases
Imagine building a website with multiple pages and trying to switch between them by manually hiding and showing sections of HTML.
You have to write lots of code to track which page is active and update the view every time the user clicks a link.
Manually managing page views is slow and confusing.
It's easy to make mistakes like showing the wrong content or forgetting to update the URL.
This leads to a poor user experience and hard-to-maintain code.
Vue Router handles all the page switching for you.
It automatically updates the URL and shows the right page component when users navigate.
This makes your app feel smooth and organized without extra manual work.
if(currentPage === 'home') { showHome(); } else if(currentPage === 'about') { showAbout(); }
import { createRouter, createWebHistory } from 'vue-router'; const router = createRouter({ history: createWebHistory(), routes: [ { path: '/home', component: Home }, { path: '/about', component: About } ] });
You can easily create multi-page apps with clean navigation and URL control.
Think of an online store where clicking categories or product links changes the page and URL smoothly without reloading the whole site.
Manual page switching is complicated and error-prone.
Vue Router automates navigation and URL updates.
This leads to better user experience and simpler code.
Practice
Solution
Step 1: Understand Vue Router's role
Vue Router is designed to help Vue apps navigate between different pages or views smoothly.Step 2: Compare with other options
Styling, data fetching, and compiling are handled by other tools, not Vue Router.Final Answer:
To manage navigation between different pages or views -> Option AQuick Check:
Vue Router = Navigation [OK]
- Confusing Vue Router with styling or data fetching tools
- Thinking Vue Router compiles components
- Assuming Vue Router manages state
Solution
Step 1: Identify Vue Router version for Vue 3
Vue 3 uses Vue Router version 4, so you must install vue-router@4.Step 2: Check other versions
Versions 2 and 3 are for Vue 2 or earlier, and version 5 does not exist yet.Final Answer:
npm install vue-router@4 -> Option BQuick Check:
Vue 3 uses Vue Router 4 [OK]
- Installing older Vue Router versions for Vue 3
- Using a non-existent version number
- Forgetting to specify version for compatibility
import { createRouter, createWebHistory } from 'vue-router';
import Home from './components/Home.vue';
import About from './components/About.vue';
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
];
const router = createRouter({
history: createWebHistory(),
routes
});
export default router;
And in App.vue template:
<router-view />
Solution
Step 1: Understand route matching
When the URL is '/about', Vue Router matches the route with path '/about' and renders the About component.Step 2: Check router-view usage
The <router-view /> in App.vue displays the matched component, so About content appears.Final Answer:
The About component content is shown -> Option DQuick Check:
URL '/about' shows About component [OK]
- Assuming Home shows for all paths
- Forgetting router-view renders matched component
- Thinking router must be manually mounted in template
import { createRouter, createWebHistory } from 'vue-router';
import Home from './components/Home.vue';
import Contact from './components/Contact.vue';
const routes = [
{ path: '/', component: Home },
{ path: '/contact', component: Contact }
];
const router = createRouter({
history: createWebHistory,
routes
});
export default router;Solution
Step 1: Check createWebHistory usage
createWebHistory is a function and must be called with parentheses: createWebHistory()Step 2: Verify other parts
Routes are defined with imported components, router is created correctly.Final Answer:
createWebHistory is missing parentheses when used -> Option CQuick Check:
Functions need parentheses to execute [OK]
- Forgetting parentheses on createWebHistory
- Believing routes array is empty
- Misnaming createRouter function
Solution
Step 1: Use correct attribute for router-link
The correct attribute to specify the route path is 'to', not 'href'.Step 2: Use correct path format
Paths must start with '/', so '/about' is correct, while 'about' without slash is incorrect.Final Answer:
Use <router-link to="/"> and <router-link to="/about"> with router-view -> Option AQuick Check:
router-link uses 'to' with leading slash paths [OK]
- Using 'href' instead of 'to' in router-link
- Omitting leading slash in paths
- Using plain anchor tags instead of router-link
Ready to prove it?
Pass the test and this pattern moves unknown
