Mastering Vue Router: A Step-by-Step Guide
What is Routing in Web Development?
Routing is the mechanism that directs HTTP requests to the code that handles them. In simpler terms, routing determines what happens when a user visits a specific page on your website. In modern JavaScript frameworks like Vue, routing has become easier to implement.
Building a Brewery App with Vue Router
In this tutorial, we’ll explore how to implement routing in a Vue app using Vue Router by building a mini demo application that showcases brewery information. We’ll be pulling data from the Open Brewery DB API.
Prerequisites
Before we dive in, make sure you have the following installed on your machine:
- Node.js 10.6.0 or above
- Yarn/npm
- Vue CLI
- VS Code
- Working knowledge of JavaScript and Vue is strongly recommended
Getting Started with Vue CLI
Let’s create a new Vue project using the Vue CLI tool. This tool allows us to select the required packages manually. Open up your terminal and type in the following command:
vue create my-brewery-app
Select the “Default (Vue 3)” preset and choose “Manually select features.” Then, select Vue Router as part of your project’s configuration.
Creating Our Views
Before we proceed, let’s create our views. Views are the pages that will be shown to users when they visit our website or application. Create the following files: AllBreweries.vue
, BreweryDetail.vue
, and NotFound.vue
inside the views
directory.
Router Setup and Configuration
Now, let’s install and configure Vue Router. Run the following command to install Vue Router:
npm install vue-router
Create a folder in the src
directory called router
and create a file named index.js
inside it. This file will serve as our router configuration file.
Lazy Loading Routes
To optimize our app’s performance, let’s implement lazy loading. Lazy loading enables us to put off loading specific routes until they are visited by the user. Update your router/index.js
file with the following code:
const AllBreweries = () => import('@/views/AllBreweries.vue')
Dynamic Routing
For our brewery app, we need to create a dynamic route that loads data from the API when a user clicks on a specific brewery. Update your router/index.js
file with the following code:
{
path: '/brewery/:id',
name: 'BreweryDetail',
component: () => import('@/views/BreweryDetail.vue')
}
Navigating Between Routes
To enable users to navigate between routes, let’s modify the code in the template section of AllBreweries.vue
. We’ll use Vue Router’s router-link
component to pass the ID retrieved from the API as a param value.
Handling 404 Errors
To handle 404 errors, let’s add a catch-all route to our router configuration file. Update your router/index.js
file with the following code:
{
path: '*',
name: 'NotFound',
component: () => import('@/views/NotFound.vue')
}
Route Transitions
To add a nice touch to our user experience, let’s implement route transitions. Update your App.vue
file with the following code:
<transition name="fade" mode="out-in">
<router-view />
</transition>
Conclusion
In this tutorial, we’ve learned how to implement routing in a Vue app using Vue Router. We’ve also explored some of Vue Router’s features that make it easy to provide a wonderful user experience. For more information on Vue Router, I recommend reading through their documentation.