π Vue Router Introduction β Navigate Your Vue Apps Like a Pro (2025 Guide)
π§² Introduction β What Is Vue Router?
Vue Router is the official routing library for Vue.js, enabling navigation between different views or pages within a Vue application. Itβs an essential tool for building Single Page Applications (SPAs) where content changes dynamically without reloading the entire page.
π― In this guide, youβll learn:
- What Vue Router is and why itβs needed
- Basic concepts: routes, links, navigation
- Key features: dynamic routing, lazy loading, nested routes
- Real-world examples and best practices
π¦ Why Do You Need Routing in Vue?
When building modern web apps, you often need:
- Multiple pages or views
- Navigation between them
- Back/forward browser support
- Deep linking via URL
Vue Router enables this by mapping URLs to components, keeping the UI and the URL in sync.
π Core Concept β What Is a Route?
A route is a mapping between a URL path and a Vue component.
β Example:
{
path: '/about',
component: AboutPage
}
When a user visits /about
, the AboutPage
component is rendered.
π§ Installing Vue Router
Use Vue CLI or manual npm installation:
npm install vue-router
π§± Setting Up Vue Router
β
router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import About from '../views/About.vue'
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
β
main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
createApp(App).use(router).mount('#app')
π§ This integrates routing into your Vue app.
π§ Navigating with <router-link>
Instead of traditional <a>
tags, use Vueβs built-in <router-link>
:
<router-link to="/about">About</router-link>
π§ This avoids page reloads and uses Vue Router to handle navigation.
π Displaying Views with <router-view>
Where do the components render? Inside the <router-view>
tag:
<template>
<router-view></router-view>
</template>
This acts as a placeholder for the current routeβs component.
𧬠Dynamic Routes
Dynamic segments allow parameterized paths:
{ path: '/user/:id', component: UserProfile }
Usage:
<router-link to="/user/42">View User 42</router-link>
Inside UserProfile.vue
, access the ID:
this.$route.params.id
π§° Nested Routes
You can nest routes inside parent components:
{
path: '/dashboard',
component: Dashboard,
children: [
{ path: 'stats', component: Stats },
{ path: 'profile', component: UserProfile }
]
}
π§ Useful for layouts that share common elements like sidebars or navbars.
π§© Lazy Loading Routes
Improve performance with code splitting:
{
path: '/about',
component: () => import('../views/About.vue')
}
Vue only loads this component when the route is visited.
π Navigation Guards
Control route access based on authentication or roles:
router.beforeEach((to, from, next) => {
if (to.meta.requiresAuth && !isLoggedIn()) {
next('/login')
} else {
next()
}
})
π« Common Mistakes
β Mistake | β Solution |
---|---|
Using <a href="..."> for routing | Use <router-link> instead |
Forgetting <router-view> | Always include it in App.vue or layout |
Mixing routes with full-page reloads | Let Vue handle SPA navigation entirely |
π Summary β Recap & Next Steps
Vue Router enables SPA navigation with clean URL handling, dynamic components, and deep linking. It forms the backbone of any multi-view Vue.js application.
π Key Takeaways:
- Map paths to components using
routes
- Use
<router-link>
for navigation - Render views using
<router-view>
- Add dynamic and nested routing as needed
βοΈ Real-World Relevance:
Vue Router is essential in building dashboards, blogs, admin panels, or any modern multi-page Vue application.
β FAQ Section
β What is Vue Router used for?
β Vue Router manages navigation and URL routing in Vue Single Page Applications (SPAs).
β How do I install and use Vue Router?
β Install via npm:
npm install vue-router
Then import and use it in your Vue app.
β What is <router-link>
in Vue?
β
It replaces <a>
for navigation:
<router-link to="/home">Home</router-link>
β What is the purpose of <router-view>
?
β Itβs a placeholder where the matched component for the route renders.
β Can I pass parameters in routes?
β
Yes. Use dynamic routes like /user/:id
and access via this.$route.params.id
.
Share Now :