Vue Routing & Navigation
Estimated reading: 4 minutes 29 views

🧭 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

PropPurpose
toDestination path
replaceReplaces history instead of pushing
active-classClass to apply when route is active
exactMatch 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Vue Routes & Links

Or Copy Link

CONTENTS
Scroll to Top