Table of contents
1.
Introduction
2.
Routing
2.1.
Advantages of Routing
3.
Setting up Router
3.1.
Using NPM
3.2.
Using Github
4.
Props for Router Link
4.1.
To
4.2.
Replace
4.3.
Append
4.4.
Tag
4.5.
Active-class
4.6.
Event
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

VueJs-Routing

Author Naman Kukreja
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Earlier, when you visited websites, they were not responsive to your request. You have to wait for a much longer time to get your result. The transactions according to your requests were not as smooth as they are now.

But in the latest websites, you do not face these problems because they use routing that resolves all these problems.

Now without wasting any time further, let's know what routing is and how to set up the router.

Routing

Routing is a method by which the user’s requests are transferred to the code which can handle them. It allows users to switch between different pages without refreshing the main page.

Advantages of Routing

  • Transactions and animations can be easily implemented for a better user experience.
  • Only the current page is rendered with no other pages, and it doesn’t refresh to load a new view.
  • Make web applications easy to debug and understand.

Setting up Router

There isn’t an inbuilt router feature in VueJs, so we have to set it up by following some additional steps.Direct Download from CDN

For downloading the latest version of vue-router, you can visit https://unpkg.com/vue-router@3.0.1/dist/vue-router.js

For the latest npm-based cdn links, you should visit unpkg.com. The link provided above always has the newest version. You can download and host it and can use it with a script tag in vue js as below:

<script src = "/path/to/vue.js"></script>
<script src = "/path/to/vue-router.js"></script>

Using NPM

To install the vue router, run the following command:

npm  install vue-router

Using Github

To clone the repository from GitHub, follows the steps stated below:

git clone https://github.com/vuejs/vue-router.git node_modules/vue-router
cd node_modules/vue-router
npm install
npm run build

Let’s start with an example

Example:

The main HTML code will look something like this:

<html>
   <head>
      <title>VueJs Instance</title>
 <script type = "text/javascript" src = "js/vue-router.js">
</script>
      <script type = "text/javascript" 
src = "js/vue.js">
</script>
   </head>
   <body>
      <div id = "app">
         <h1>Examples of Routing</h1>
         <p>
            <router-link to = "/route1">1st Router Link</router-link>
            <router-link to = "/route2">2nd Router Link </router-link>
         </p>
         <!-- route outlet -->
<!-- The component which are matched by route are visible here -->
         <router-view></router-view>
      </div>
      <script type = "text/javascript">
       
         const Route1 = { template: '<div style = "border-radius:20px;background-color:green;height:50px;width:200px;margin:10px;font-size:25px;padding:10px;"> router 1</div>' }
         const Route2 = { template: '<div style = "border-radius:20px;background-color:cyan;margin:10px;width:200px;height:50px;font-size:25px;padding:10px;"> router 2</div>' }
         const routes = [
            { path: '/route1', component: Route1 },
            { path: '/route2', component: Route2 }
         ];
         const router = new VueRouter({
            routes // short for `routes: routes`
         });
         var vm = new Vue({
            el: '#app',
            router
         });
      </script>
   </body>
</html

Take the code from https://unpkg.com/vue-router@3.0.1/dist/vue-router.js, create a vue-router.js file, and copy the code in that file and save it.

After vue js the script is added as follows:

<script type = "text/javascript" src = "js/vue.js"></script>
<script type = "text/javascript" src = "js/vue-router.js"></script>

A router link is defined in the body section:

<p>
   <router-link   to = "/route1">Router Link 1</router-link>
   <router-link    to = "/route2">Router Link 2</router-link>
</p>

The route defines the paths and components. When the user clicks on the router link, URL '/route1' will be displayed.

The router link needs to be matched with the path from the routes. The template's name to be displayed is taken by components.

Example:

<router-link to = "path here"></router-link> 

Next route is created in VueRouter

const router = new VueRouter({
   routes // short for `routes: routes`
});

The constructor of VueRouter takes routes as the param. The router object assigns the main value instance.

var vm = new Vue({
   el: '#app',
   router
})

 

Check out most important Git Interview Questions here.

Props for Router Link

We will learn about some of the more properties passed to <router-link>.

To

It refers to the destination path given to <router-link>. The value of to is passed internally to the router.push() when clicked. The value needs to be a location object or string. We need to bind the object when using it.

e.g. 1:  <router-link to = "/route1">Router Link 1</router-link>
renders as
<a href = "#/route">Router Link </a>
e.g. 2:  <router-link v-bind:to = "{path:'/route1'}">Router Link 1</router-link>
e.g. 3: <router-link v-bind:to =
   "{path:'/route1', query: { name: 'Tery' }}">Router Link 1</router-link>//router link with query string.

name= Tery in the url is the part of the query string

Replace

The router.replace() function is called when replace is added in the router link. The navigation history is not stored with replace.

<router-link v-bind:to = "{path:'/route1', query: { name: 'Tery' }}"   replace>Router Link 1</router-link>

Append

It will make the path relative when you add append to the <router link>. If we want to go from route1 to route2 then it will show as route1/route2.

<router-link v-bind:to = "{ path: '/route1'}" append>Router Link 1</router-link>

Tag

Presently <router-link> is rendered as a tag but if you want to render it as some other tag can use by specifying tag=”Tagname”.

<p>
   <router-link v-bind:to = "{ path: '/route1'}" tag = "span">Router Link 1</router-link>
   <router-link v-bind:to = "{ path: '/route2'}" tag = "span">Router Link 2</router-link>
</p>

Active-class

router-link-active is the default active class when router link is active. We can change this by following the code below:

<style>
   ._active{
      background-color : red;
   }
</style>
<p>
   <router-link v-bind:to = "{ path: '/route1'}" active-class = "_active">Router Link 1</router-link>
   <router-link v-bind:to = "{ path: '/route2'}" tag = "span">Router Link 2</router-link>
</p> 

Event

Click event is the default event for router-link. By event property, we can change the same.

<router-link v-bind:to = "{ path: '/route1'}" event = "mouseover">Router Link 1</router-link>

FAQs

  1. What are the advantages of using Vuejs?
    It is small in size, easy to understand, simple integration, components, flexibility.
     
  2. Does Vuejs have an inbuilt router feature?
    No, Vuejs doesn't have an in-built router feature.
     
  3. What is the default active class in the router link?
    router-link-active is the default active class in the router link.
     
  4. State one advantage of routing?
    It allows us to move between different pages without refreshing the main page.

Key Takeaways

We have learned about routing in Vuejs in this blog and its advantages. We have also known about setting up the router by various methods and props for router links.

If you want to learn about change detection caveat in vue2 then you must have a look here.

Live masterclass