que-es-vue-router

What is Vue Router and how to use it

  • 4 min

Vue Router is the official routing library for Vue.js. It allows us to define routes in a Vue.js application and associate each route with a specific component.

Vue Router is the fundamental component for creating single-page applications (SPA) where navigation between views happens without reloading the page.

When the user navigates to a specific URL, Vue Router loads the corresponding component, giving the sensation that “the page has changed”.

Main Features

  • Dynamic Routes: Allows defining routes with dynamic parameters
  • Programmatic Navigation: Change routes via JavaScript code
  • Route Guards: Control access to specific routes
  • Lazy Loading: Load components lazily to improve performance

Installing Vue Router

To start using Vue Router, we first need to install it in our project (logical 😜). If you don’t have it installed, you can easily add it.

npm install vue-router

Basic Configuration

Once installed, we need to configure Vue Router in our application. This involves touching several files. Let’s see it step by step.

Create a route configuration file. For example router.js in the src folder of your project.

// src/router.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;
Copied!

Here we establish the relationship between URL and component to render.

Now we have to import and use the router in the application. To do this, in your application’s main file (main.js or main.ts), import the file we just created.

// src/main.js
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';

const app = createApp(App);
app.use(router);
app.mount('#app');
Copied!

Finally, we add the <RouterView> component to our main App.vue file,

<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
</script>

<template>
  <RouterView />
</template>
Copied!

This component is responsible for rendering the views (its content will be replaced by the component we navigate to).

Creating Basic Routes

Routes in Vue Router are defined as an array of objects, where each object represents a route.

Each route has two main properties:

  • path: The URL that will trigger the route.
  • component: The component that will be rendered when the route is accessed
const routes = [
    { path: '/', component: Home },
    { path: '/about', component: About },
    { path: '/contact', component: Contact },
];
Copied!

To navigate between pages in a Vue.js application with Vue Router we will use the <RouterLink> component. This component provides a way to create navigation links.

It’s similar to the <a> tag in HTML, but designed to work with Vue Router.

When clicking on a <RouterLink>, Vue Router loads the component associated with the route without reloading the page.

<template>
  <nav>
    <RouterLink to="/">Home</RouterLink>
    <RouterLink to="/about">About</RouterLink>
    <RouterLink to="/contact">Contact</RouterLink>
  </nav>
</template>
Copied!
  • to: Specifies the destination route (equivalent to href in an HTML link).

Active Styles

You can use the .router-link-active class (or .router-link-exact-active for exact matches) to visually highlight the link of the current page.

We can also use props like active-class to define custom CSS classes when the route is active:

<RouterLink to="/about" active-class="active-link">
  About
</RouterLink>
Copied!

Lazy Loading Components

It is also possible to load components lazily. This means the component will only load when its route is accessed (instead of on initial load).

This helps improve application performance, especially when it has many components, or they are very heavy, or some of them are not visited frequently.

const routes = [
    {
        path: '/about',
        component: () => import('./views/About.vue'),
    },
];
Copied!

Don’t overuse it either, because it also has its drawbacks (the component has to load).

Use it when one of the conditions I mentioned applies.