BeginnerCoder
beginner#Vue.js#Vue Router#Client-side Routing

Creating a Vue.js Application with Vue Router

This article was AI-drafted and has not been reviewed yet. Spotted a mistake? Tell us.

Learn how to create a Vue.js application with client-side routing using Vue Router.

Introduction to Vue Router

In this tutorial, we will learn how to create a Vue.js application with client-side routing using Vue Router. This will allow us to navigate between different pages in our application without requiring a full page reload.

Step 1: Installing Vue Router

First, we need to install Vue Router in our project. We can do this by running the following command in our terminal:

npm install vue-router

Step 2: Creating Routes

Next, we need to create routes for our application. We can do this by creating a new file called router.js in the src directory:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from './views/Home.vue'
import About from './views/About.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/about',
    name: 'about',
    component: About
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

Step 3: Adding Routes to the App

Finally, we need to add the routes to our App component. We can do this by modifying the main.js file:

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

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

Now we can run our application using npm run serve and see our routes in action.

Keep going with Vue.js

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.