Client-Side Routing in Vue | asadmukhtar.info
Step-by-Step Guide to Setting Up Authentication in Laravel 12 with Breeze   |   Manual Authentication in Laravel 12: Step-by-Step Guide   |   How to Build a REST API in Laravel 12 with Sanctum   |   Laravel 12 CRUD Application with Image Upload   |   Laravel 12 Multi-Auth System: Admin & User Login   |   How to Integrate Stripe Payment Gateway in Laravel 12   |   Building a Role-Based Access Control (RBAC) in Laravel 12   |   How to Use Laravel 12 Queues and Jobs for Background Tasks   |   Laravel 12 Livewire CRUD Example with Validation   |   Email Verification and Password Reset in Laravel 12   |   How to Use Laravel 12 API with Vue.js 3   |   Laravel 12 AJAX CRUD with jQuery and Bootstrap   |   Laravel 12 Multi-Language Website Setup   |   React Best Practices for 2025: Performance, SEO, and Scalability   |   How to Build a Full-Stack MERN App: A Step-by-Step Guide   |   React State Management: Redux vs. Context API vs. Recoil   |   Server-Side Rendering (SSR) in React with Next.js for SEO   |   How to Optimize React Apps for Faster Load Times   |   Building a REST API with Node.js and Express for a React App   |   Integrating JWT Authentication in React and Node.js (MERN Stack)   |   Real-time Chat App with React, Node.js, and Socket.io   |   How to Deploy a MERN Stack Application on AWS or Vercel   |   Connecting React Frontend to a Node.js Backend with Axios   |   Laravel Implement Flash Messages Example   |   How to integrate Angular 19 with Node.js and Express for full-stack development   |   Best practices for connecting Angular 19 frontend with Laravel API   |   Step-by-step guide to upgrading an existing project to Angular 19   |   How to implement authentication in Angular 19 using JWT and Firebase   |   Optimizing server-side rendering in Angular 19 with route-level render modes   |   Using Angular 19 signals for state management in large applications   |   How to create standalone components in Angular 19 for modular architecture   |   Building a CRUD application in Angular 19 with MongoDB and Express   |   Implementing lazy loading in Angular 19 to improve performance   |   How to integrate Angular 19 with GraphQL for efficient data fetching   |   Vue 3 Composition API vs Options API: A Comprehensive Comparison   |   Fetching and Displaying Data from APIs in Vue.js with Axios   |   Building a Todo App in Vue.js with Local Storage Integration   |   Handling Forms and Validation in Vue.js Using VeeValidate   |   State Management in Vue.js Applications Using Vuex   |   10 Most Important Tasks Every MERN Stack Developer Should Master   |   How to Build a Full-Stack CRUD App with MERN Stack   |   Best Practices for Authentication & Authorization in MERN Stack   |   1. MEAN Stack vs. MERN Stack: Which One Should You Choose in 2025   |   Top 10 Node.js Best Practices for Scalable and Secure Applications   |   How to Build a REST API with Laravel and Node.js (Step-by-Step Guide)   |   Mastering Angular and Express.js for Full-Stack Web Development   |   Top 10 Daily Tasks Every Frontend Developer Should Practice   |   Essential Backend Development Tasks to Boost Your Coding Skills   |   Real-World Mini Projects for Practicing React.js Daily   |   Laravel Developer Task List: Beginner to Advanced Challenges   |   How to Assign Effective Tasks to Your Intern Developers   |   10 Must-Try Tasks to Master JavaScript Fundamentals   |   Practical CSS Challenges That Improve Your UI Design Skills   |   Top Tasks to Learn API Integration in React and Angular   |   Best Task Ideas for a 30-Day Web Development Challenge   |   Top Git and GitHub Tasks Every Developer Should Know   |   30-Day Task Plan for Web Development Interns   |   Weekly Task Schedule for Junior Developers in a Startup   |   How to Track Progress with Development Tasks for Interns   |   What Tasks Should You Give to Interns in a MERN Stack Project   |   Build These 5 Projects to Master React Routing   |   Task-Based Learning: Become a Full-Stack Developer in 90 Days   |   Daily Coding Tasks That Will Sharpen Your Logical Thinking   |   Top 7 Backend Task Ideas to Practice With Node.js and MongoDB   |  

1. What is Client-Side Routing?

Client-side routing allows an application to update the URL and display the appropriate view (or page) without needing to reload the entire page from the server. In single-page applications (SPAs), this is achieved by dynamically changing the content based on the URL.

  • No page reloads: When you navigate between views, only the content changes, not the entire page.
  • Faster user experience: Since the page doesn't reload, the user experience is more fluid and faster.
  • URL management: The browser's URL changes to reflect the current view, allowing users to bookmark pages and navigate using the browser's back and forward buttons.

2. Setting Up Vue Router

To enable client-side routing in your Vue application, you'll need to install Vue Router and configure it. Here’s how to set it up step by step:

Step 1: Install Vue Router

First, you need to install Vue Router in your Vue.js project. Run the following command in your project directory:

npm install vue-router

Step 2: Configure Vue Router

After installation, you need to set up Vue Router in your project. Create a router.js file where you'll define the routes and their corresponding components.

// src/router.js
import Vue from 'vue';
import Router from 'vue-router';
import Home from './components/Home.vue';
import About from './components/About.vue';

Vue.use(Router);

export default new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/about',
      name: 'about',
      component: About
    }
  ]
});
  • Vue.use(Router) enables Vue Router in your Vue instance.
  • routes is an array where you define the route paths and the components to load for those paths.

3. Integrating the Router with Vue Instance

Once you’ve configured the router, you need to tell your main Vue instance to use the router. In your main.js or app.js file, import the router and add it to the Vue instance.

// src/main.js
import Vue from 'vue';
import App from './App.vue';
import router from './router';

new Vue({
  render: h => h(App),
  router
}).$mount('#app');

In this code:

  • router is imported and added to the Vue instance.
  • The App.vue file will contain a <router-view> element that will act as a placeholder for the matched components.

4. Using router-view to Render Components

In your App.vue, use the <router-view> tag to display the component corresponding to the current route. This is where the content changes dynamically based on the URL.

<!-- src/App.vue -->
<template>
  <div id="app">
    <h1>Welcome to My Vue App</h1>
    <nav>
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link>
    </nav>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'App'
};
</script>

Explanation:

  • The <router-link> tag is used for navigation. It works similarly to an anchor tag (<a>) but without reloading the page.
  • <router-view> is a placeholder that displays the component associated with the current route.

5. Navigating Between Views

With the basic setup done, you can now navigate between views in your Vue application. Vue Router handles the URL changes and displays the corresponding components dynamically.

  • Home View: When the user navigates to /, the Home.vue component will be rendered inside <router-view>.
  • About View: When the user navigates to /about, the About.vue component will be displayed.

You can also programmatically navigate between routes using the router instance in methods.

Example of Programmatic Navigation:

<template>
  <div>
    <button @click="goToAbout">Go to About</button>
  </div>
</template>

<script>
export default {
  methods: {
    goToAbout() {
      this.$router.push('/about');
    }
  }
};
</script>

6. Dynamic Routes

Vue Router also allows you to create dynamic routes. You can pass dynamic parameters to the route and use them in your components.

Example of a Dynamic Route:

// src/router.js
import Post from './components/Post.vue';

export default new Router({
  routes: [
    {
      path: '/post/:id',
      name: 'post',
      component: Post
    }
  ]
});

Using Dynamic Parameters in the Component:

<!-- src/components/Post.vue -->
<template>
  <div>
    <h2>Post ID: {{ $route.params.id }}</h2>
  </div>
</template>

<script>
export default {
  name: 'Post'
};
</script>

7. Nested Routes

Vue Router supports nested routes, where you can have a route inside another route. This is useful for creating complex layouts.

Example of Nested Routes:

// src/router.js
import Dashboard from './components/Dashboard.vue';
import Profile from './components/Profile.vue';

export default new Router({
  routes: [
    {
      path: '/dashboard',
      component: Dashboard,
      children: [
        {
          path: 'profile',
          component: Profile
        }
      ]
    }
  ]
});

Using Nested Routes:

In the Dashboard.vue component, you would have a <router-view> to display the nested route content.

<!-- src/components/Dashboard.vue -->
<template>
  <div>
    <h2>Dashboard</h2>
    <router-link to="/dashboard/profile">Go to Profile</router-link>
    <router-view></router-view>  <!-- Nested route will render here -->
  </div>
</template>

8. Summary of Key Points:

  • Vue Router enables client-side routing in Vue.js for single-page applications.
  • The v-on directive allows navigation between different views with dynamic route handling.
  • <router-link> is used for navigating between routes without page reloads.
  • <router-view> is a placeholder for rendering the active component based on the current route.
  • Dynamic routes allow you to pass parameters, making your app more flexible.
  • Nested routes enable complex layouts where components can have their own sub-routes.

9. Conclusion

Client-side routing with Vue.js, powered by Vue Router, is a powerful way to manage navigation in your single-page applications. By mastering Vue Router, you can create dynamic, responsive, and user-friendly applications that provide a seamless experience for users. From basic navigation to complex route setups with dynamic and nested routes, Vue Router simplifies routing in your Vue apps, making it easy to manage different views and components.