π§ Vue Routes & Links β Define Paths and Navigate Seamlessly (2025 Guide)
π§² Introduction β What Are Routes and Links in Vue?
In Vue.js, routes determine which component gets rendered based on the URL path, while links (<router-link>
) let users navigate between those paths without reloading the page. Together, they form the backbone of navigation in Single Page Applications (SPAs) using Vue Router.
π― In this guide, youβll learn:
- How to define and configure routes
- How to create navigation links with
<router-link>
- Dynamic and nested route techniques
- Real-world examples and best practices
π What Is a Route?
A route is a mapping between a specific URL path and a Vue component.
β Example Route Definition:
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
];
π§ When the URL is /about
, the About
component renders in <router-view>
.
π§± Creating Routes in Vue
β Step 1: Install Vue Router
npm install vue-router
β Step 2: Setup the Router
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;
β Step 3: Use the Router in Your App
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
createApp(App).use(router).mount('#app');
π What Is <router-link>
?
The <router-link>
tag is a Vue component that replaces traditional <a>
tags for navigation. It ensures that navigation happens without reloading the page.
β Example:
<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>
π§ It binds to the Vue Router’s navigation system.
π§° <router-link>
Props and Customization
Prop | Purpose |
---|---|
to | Destination path |
replace | Replaces history instead of pushing |
active-class | Class to apply when route is active |
exact | Match path exactly |
β Example with Replace and Custom Class
<router-link to="/dashboard" replace active-class="active-link">
Dashboard
</router-link>
π Displaying Routed Components with <router-view>
The <router-view>
tag is where your routed components get rendered.
β Example:
<template>
<nav>
<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>
</nav>
<router-view />
</template>
𧬠Dynamic Routes with Parameters
You can create routes that accept dynamic values.
β Route Definition:
{ path: '/user/:id', component: UserProfile }
β Link:
<router-link :to="`/user/${user.id}`">Profile</router-link>
β Access in Component:
this.$route.params.id
π§© Nested Routes (Child Routes)
You can create parent-child relationships in routes.
β Example:
{
path: '/admin',
component: AdminLayout,
children: [
{ path: 'users', component: UserList },
{ path: 'settings', component: AdminSettings }
]
}
π§ <router-view>
inside AdminLayout
will render the child components.
π Summary β Recap & Next Steps
Vue Routes and Links power smooth, SPA-style navigation in Vue apps. By defining routes and using <router-link>
, developers can create dynamic, interactive, and well-structured navigation systems.
π Key Takeaways:
- Define routes with paths and components
- Use
<router-link>
for seamless navigation <router-view>
renders the matched component- Use dynamic and nested routes for advanced cases
βοΈ Real-World Relevance:
Routes and links are used in dashboards, blogs, multi-page apps, and navigation-heavy layouts.
β FAQ Section
β What is the purpose of <router-link>
in Vue?
β It provides client-side navigation without page reloads using Vue Router.
β Can I navigate programmatically instead of using <router-link>
?
β
Yes. Use this.$router.push('/about')
or this.$router.replace()
inside methods.
β What is <router-view>
used for?
β Itβs a placeholder where Vue dynamically renders the matched route component.
β How do dynamic routes work?
β
Define with /path/:param
, then access via this.$route.params.param
.
β How do I add active styles to router links?
β
Use active-class
prop or the default .router-link-active
class in CSS.
Share Now :