In this section, we will explore nested routes in Vue Router. Nested routes allow you to create complex routing structures by nesting child routes within parent routes. This is particularly useful for creating multi-level navigation in your Vue.js applications.
Key Concepts
- Parent and Child Routes: Define routes within other routes to create a hierarchy.
- Nested
<router-view>
: Use multiple<router-view>
components to display nested routes. - Route Configuration: Configure nested routes in the Vue Router instance.
Setting Up Nested Routes
Step 1: Install Vue Router
If you haven't already installed Vue Router, you can do so using npm or yarn:
Step 2: Define Nested Routes
Let's define a set of nested routes. Consider a scenario where you have a User
component with nested routes for Profile
and Posts
.
// router/index.js import Vue from 'vue'; import Router from 'vue-router'; import User from '@/components/User.vue'; import UserProfile from '@/components/UserProfile.vue'; import UserPosts from '@/components/UserPosts.vue'; Vue.use(Router); const routes = [ { path: '/user/:id', component: User, children: [ { path: 'profile', component: UserProfile }, { path: 'posts', component: UserPosts } ] } ]; const router = new Router({ routes }); export default router;
Step 3: Create Components
Create the User
, UserProfile
, and UserPosts
components.
<!-- User.vue --> <template> <div> <h2>User {{ $route.params.id }}</h2> <router-view></router-view> <!-- Nested <router-view> --> </div> </template> <script> export default { name: 'User' }; </script>
<!-- UserProfile.vue --> <template> <div> <h3>User Profile</h3> <p>This is the user profile page.</p> </div> </template> <script> export default { name: 'UserProfile' }; </script>
<!-- UserPosts.vue --> <template> <div> <h3>User Posts</h3> <p>This is the user posts page.</p> </div> </template> <script> export default { name: 'UserPosts' }; </script>
Step 4: Use Nested Routes in the Application
Update your main application file to use the router.
<!-- App.vue --> <template> <div id="app"> <router-link to="/user/123/profile">Go to User Profile</router-link> <router-link to="/user/123/posts">Go to User Posts</router-link> <router-view></router-view> </div> </template> <script> export default { name: 'App' }; </script>
Step 5: Run the Application
Run your Vue.js application to see the nested routes in action.
Practical Exercise
Exercise: Create Nested Routes for a Blog
- Objective: Create a
Blog
component with nested routes forPostList
andPostDetail
. - Steps:
- Define the
Blog
,PostList
, andPostDetail
components. - Configure nested routes in the Vue Router instance.
- Use nested
<router-view>
to display the nested routes.
- Define the
Solution
// router/index.js import Vue from 'vue'; import Router from 'vue-router'; import Blog from '@/components/Blog.vue'; import PostList from '@/components/PostList.vue'; import PostDetail from '@/components/PostDetail.vue'; Vue.use(Router); const routes = [ { path: '/blog', component: Blog, children: [ { path: '', component: PostList }, { path: ':postId', component: PostDetail } ] } ]; const router = new Router({ routes }); export default router;
<!-- Blog.vue --> <template> <div> <h2>Blog</h2> <router-view></router-view> <!-- Nested <router-view> --> </div> </template> <script> export default { name: 'Blog' }; </script>
<!-- PostList.vue --> <template> <div> <h3>Post List</h3> <p>This is the post list page.</p> </div> </template> <script> export default { name: 'PostList' }; </script>
<!-- PostDetail.vue --> <template> <div> <h3>Post Detail</h3> <p>This is the post detail page for post {{ $route.params.postId }}.</p> </div> </template> <script> export default { name: 'PostDetail' }; </script>
Summary
In this section, we learned how to set up nested routes in Vue Router. We covered:
- Defining parent and child routes.
- Using nested
<router-view>
components. - Configuring nested routes in the Vue Router instance.
Nested routes are a powerful feature that allows you to create complex and hierarchical navigation structures in your Vue.js applications. In the next section, we will explore navigation guards to control access to routes.
Vue.js Course
Module 1: Introduction to Vue.js
- What is Vue.js?
- Setting Up the Development Environment
- Creating Your First Vue Application
- Understanding the Vue Instance
Module 2: Vue.js Basics
- Template Syntax
- Data Binding
- Computed Properties and Watchers
- Class and Style Bindings
- Conditional Rendering
- List Rendering
Module 3: Vue.js Components
- Introduction to Components
- Props and Custom Events
- Slots
- Dynamic and Async Components
- Component Communication
Module 4: Vue Router
Module 5: State Management with Vuex
- Introduction to Vuex
- State, Getters, Mutations, and Actions
- Modules in Vuex
- Using Vuex in Components
- Advanced Vuex Patterns
Module 6: Vue.js Directives
Module 7: Vue.js Plugins
Module 8: Testing in Vue.js
Module 9: Advanced Vue.js Concepts
- Render Functions and JSX
- Server-Side Rendering (SSR) with Nuxt.js
- Vue 3 Composition API
- Performance Optimization