Creating and Navigating Between Pages

Tutorial 2 of 5

Introduction

Goal

In this tutorial, we'll learn how to create routes and navigation links in a Vue.js application. By the end of the tutorial, you'll be able to define pathways and create links that users can use to navigate different parts of your application.

Learning Outcome

You will understand how to:

  1. Define routes in Vue.js
  2. Create navigational links
  3. Navigate between different pages or components in your Vue.js application

Prerequisites

Before you start this tutorial, you should have:

  1. Basic knowledge of Vue.js
  2. Vue.js installed on your local development environment
  3. A text editor, such as VS Code, installed
  4. Basic understanding of HTML, CSS, and JavaScript

Step-by-Step Guide

Vue Router

Vue Router is the official router for Vue.js. It deeply integrates with Vue.js core to make building Single Page Applications with Vue.js a breeze.

To install Vue Router, open your terminal and run:

npm install vue-router

Now, let's dive into creating routes and navigation links.

Creating Routes

In Vue.js, you define your routes in a routes array. Each route should map to a Vue component. Here's an example of a simple routes array:

const routes = [
  { path: '/', component: HomeComponent },
  { path: '/about', component: AboutComponent }
]

Navigation Links

Vue Router provides a <router-link> component for creating links. When the link is clicked, the router-view is updated. Here's an example:

<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>

Code Examples

Example 1: Defining Routes

Here's a practical example of how you might define routes in a Vue.js application.

// Import Vue and VueRouter
import Vue from 'vue'
import VueRouter from 'vue-router'

// Import your components
import HomeComponent from './components/HomeComponent.vue'
import AboutComponent from './components/AboutComponent.vue'

// Tell Vue to use VueRouter
Vue.use(VueRouter)

// Define your routes
const routes = [
  { path: '/', component: HomeComponent },
  { path: '/about', component: AboutComponent }
]

// Create the router instance and pass the `routes` option
const router = new VueRouter({
  routes // short for `routes: routes`
})

// Create and mount the root instance.
// Make sure to inject the router with the router option to make the
// whole app router-aware.
const app = new Vue({
  router
}).$mount('#app')

Example 2: Creating Navigation Links

When you've defined your routes, you can create navigation links using the <router-link> component.

<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>

<!-- this is where your components will be rendered -->
<router-view></router-view>

When you click the links, the corresponding component will be rendered in the <router-view>.

Summary

In this tutorial, we've learned how to create routes and navigation links in a Vue.js application. You've learned how to define routes, how to create links, and how to navigate between different parts of your application.

Next, I recommend you learn how to create nested routes and how to pass parameters to routes.

Practice Exercises

  1. Create a Vue.js application with three pages: Home, About, and Contact. Define routes for each page and create navigation links.

  2. Add a nested route to your About page. For example, create an 'Our Team' page that is a child of the About page.

  3. Pass a parameter to one of your routes and display it in your component. For example, pass a user ID to your 'User Profile' page and display the user's information.

Solutions

Please refer to the Vue Router documentation for detailed explanations and solutions.

Remember, the best way to learn is by doing. Keep practicing and you'll get the hang of it!