intermediate#vue#vue router#client-side routing
Using Vue Router to Create a Multi-Page App
Learn how to use Vue Router to create a multi-page app with client-side routing.
Introduction to Vue Router
Vue Router is the official router for Vue.js. It allows us to create client-side routing and navigate between different pages in our app. In this tutorial, we will learn how to use Vue Router to create a multi-page app.
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 app. We can do this by creating a new file called router.js and defining our routes:
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: Navigating Between Routes
Finally, we need to navigate between our routes. We can do this by using the router-link component:
<template>
<div>
<h1>Home</h1>
<router-link to="/about">Go to About</router-link>
</div>
</template>