Using Dynamic and Nested Routes

Tutorial 3 of 5

1. Introduction

The goal of this tutorial is to provide you with a clear understanding of how to use dynamic and nested routes in Vue.js. This tutorial aims to help you create more flexible and organized applications using Vue's powerful routing capabilities.

By the end of this tutorial, you will be able to:

  • Create dynamic routes that can change based on user input
  • Implement nested routes to build hierarchical site structures
  • Understand how these concepts can be applied in real-world applications

Prerequisites:

  • Basic understanding of Vue.js
  • Familiarity with JavaScript and HTML

2. Step-by-Step Guide

Dynamic Routes

Dynamic routes are routes that can change based on user input or other factors. In Vue.js, you can create dynamic routes by using the :id syntax in your route path.

{
  path: '/user/:id',
  component: User
}

In this case, :id is a placeholder for whatever value is passed in the URL after /user/.

Nested Routes

Nested routes allow you to create more complex interfaces where certain parts of your app are persistent while others change dynamically. This is achieved by creating "child" routes within "parent" routes.

{
  path: '/user/:id',
  component: User,
  children: [
    {
      path: 'profile',
      component: UserProfile
    },
    {
      path: 'posts',
      component: UserPosts
    }
  ]
}

In this example, UserProfile and UserPosts are child routes of the User parent route. When the /user/:id/profile or /user/:id/posts URLs are visited, the corresponding child component will be displayed.

3. Code Examples

Example 1: Dynamic Route

// Define your User component
const User = {
  template: '<div>User {{ $route.params.id }}</div>'
}

// Define your route
const routes = [
  { path: '/user/:id', component: User }
]

// Create the router instance
const router = new VueRouter({
  routes
})

// Create and mount the root instance.
const app = new Vue({
  router
}).$mount('#app')

// Now the app has started!

When you visit /user/123, the rendered HTML will be <div>User 123</div>.

Example 2: Nested Route

// Define your components
const User = {
  template: `
    <div class="user">
      <h2>User {{ $route.params.id }}</h2>
      <router-view></router-view>
    </div>
  `
}

const UserProfile = { template: '<div>Profile</div>' }
const UserPosts = { template: '<div>Posts</div>' }

// Define your routes
const routes = [
  { 
    path: '/user/:id', 
    component: User,
    children: [
      {
        path: 'profile',
        component: UserProfile
      },
      {
        path: 'posts',
        component: UserPosts
      }
    ]
  }
]

// Create the router instance
const router = new VueRouter({
  routes
})

// Create and mount the root instance.
const app = new Vue({
  router
}).$mount('#app')

// Now the app has started!

When you visit /user/123/profile, the rendered HTML will be the User component with the UserProfile component nested inside.

4. Summary

In this tutorial, we've learned how to create dynamic routes and nested routes in Vue.js. These concepts allow us to create more flexible and structured applications.

Further learning can include exploring more advanced routing concepts in Vue.js, such as named routes and route guards. You can find more information in the Vue Router documentation.

5. Practice Exercises

  1. Create a dynamic route for a Blog component where each post can be accessed with a unique :id.

  2. Build on the previous exercise by adding nested routes for comments and author under each blog post.

  3. Experiment with adding route guards to restrict access to certain routes.

Solutions and further practice can be found by exploring the Vue Router documentation and experimenting with different routing configurations in your own Vue.js applications.