Open In App

Vue.js | Routing

Routing is one of the many features provided by Vue.js to allow users to switch between pages without refreshing every time a page is loaded. This results in smooth transitions between pages giving a better feel for the user.
Setting Up The Application: Firstly, we need to create a project to work on. We can use Vue CLI (Command Line Interface) for this. With the terminal open, run the following below command. 
 

vue init webpack vue-routing-example

During the initialization process, make sure to say yes when prompted to install the vue-router. It is required in order to work with routing in our application. After the initialization is over, open up the application to check if the process was successful by running the following command in the terminal: 
 



cd vue-routing-example
npm run dev

With those commands, your application would be built and deployed into the local development server. 
href=”http://localhost:8080″
Building Our Application: In order to implement routing in our application, we must first create pages to route to. We can change and use the HelloWorld.vue file as our first page and create a new file in that component folder HelloGeek.vue as our second page. These pages will have nothing functional and will be for understanding the flow of routing. 
 




<template>
  <div class = 'helloworld'>
    <h1>{{ msg }}</h1>
    </div>
</template>
 
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Hello World!'
    }
  }
}
</script>

<router-link to="/helloworld">Hello World</router-link>



Article Tags :