Vue Routing & Navigation
Estimated reading: 4 minutes 217 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 :
Share

Vue Routes & Links

Or Copy Link

CONTENTS
Scroll to Top