beginner#Vue.js#Vue Router#Client-side Routing
Creating a Vue.js Application with Vue Router
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.