Vue.js Router

Vue.js is a reactive javascript framework, which is used to build UIs(User Interfaces) and SPAs(Single-page Applications) and developers love to code and feel freedom and comfort while developing applications in Vue.js. For routing purposes, Vue.js does not provide the built-in routing feature. But there is an official third party library with the name of Vue Router for providing this feature. By using this feature we can navigate between the web pages but without reloading. So, in this article, we are going to see how we can install and use Vue Router in Vue.js.


We can install the Vue router into an existing Vue.js project by running the following command in the terminal

npm install vue-router

After a successful installation, we need to import VueRouter in the main.js file in the src directory as well using the following syntax

import Vue from 'vue'
import router from './router'


After importing the router, you are good to go and use vue-router in your project.

But if you are installing Vue.js using Vue CLI. You won’t need this extra installation step. You can add a vue-router plugin during selecting a preset.


The usage of the vue-router is very simple and easy to use. First, in the template or HTML

<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
<router-view />

In this pretty simple and clear example of vue-router. We have created simple navigation using router-link components and provide the link using the prop named ‘to’. The router-link works the same as an anchor ‘a’ tag. It is actually rendered as an ‘a’ tag by default. In the router-view, we will have the relative component which matches the route.

In the javascript, we first have to register and import the components to define their routes. We suppose that we have a component named Comp.vue in the views directory to which we will import in the router’s index.js file in the router directory and define it as a route.

To import a component, we use the following statement

import Comp from "../views/Comp.vue";

After importing, we have to define the route now and map it to the component. Like this,

const routes = [


path: "/",

name: "Comp",

component: Comp



We can give multiple routes too, separated by a comma. Like this,

const routes = [


path: "/",

name: "Comp",

component: Comp



path: "/comp2",

name: "Comp2",

component: Comp2



After defining the routes. Pass routes array to the router instances. So, let’s create the router instance as well

const router = createRouter({

routes // short for `routes: routes`


In the end, in the main.js file. We have to create the root instance and mount that as well and inject the routes in it so that the whole app becomes aware of the routes.




By using this injection technique. We can access the router in any component, using this.$router.

We can now programmatically push routes at the click of a button or anything you want, instead of using the router-link component. For example,

methods: {

clickFunc() {




Wrapping up and summary

In this article, we have learned to install Vue Router using different ways and learned to use Vue router programmatically in javascript and in the Vue.js’s template. We have also learned to set up the Vue Router in an existing project in a very easy and step by step detailed guide. If you want to learn more about the Vue Router, kindly visit Vue Router: Official Docs.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.