Introduction to Vue.js Routing

Vue.js allows you to create single-page applications (SPAs) with ease using its built-in routing capabilities. Routing enables you to navigate between different views without the need for full-page refreshes. In this guide, we'll explore how to set up and use Vue.js routing to build SPAs.

Setting Up Vue Router

To use Vue.js routing, you need to set up Vue Router, which is the official routing library for Vue.js. You can include Vue Router via a CDN or use a build tool like Vue CLI for more complex projects. Here's an example of including Vue Router using a CDN:

<script src=""></script>
<script src=""></script>

Make sure to include both Vue.js and Vue Router in your HTML file.

Configuring Vue Router

Once Vue Router is included, you need to configure it by defining your routes. Routes map URL paths to components. Here's an example of configuring Vue Router:

const Home = { template: '<div>Home Page</div>' };
const About = { template: '<div>About Page</div>' };
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
const router = new VueRouter({
new Vue({
el: '#app',

In this example, we define two routes, one for the home page and one for the about page. The `router` object is created with these routes, and it's attached to the Vue instance.

Creating a Router View

To display routed components, you need a `` element in your template. This element is where the component matching the current route will be rendered. Here's an example:

<div id="app">

This `` element is where the matched component will be displayed based on the current route.


Vue.js routing with Vue Router allows you to build dynamic and responsive single-page applications. With Vue Router, you can easily define routes, map them to components, and provide a smooth navigation experience for your users.