vue-router-best-practices

Apply Vue Router guard and lifecycle patterns for Vue 3 SPA navigation.

11|2|Updated Jan 15, 2026
One-click install
npx skills add https://github.com/runkids/my-skills --skill vue-router-best-practices
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vue-router-best-practices
Source: https://github.com/runkids/my-skills/tree/main/vue-router-best-practices
Command: npx skills add https://github.com/runkids/my-skills --skill vue-router-best-practices

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This skill consolidates Vue Router best practices, patterns, and guard usage for reliable navigation in Vue 3 SPAs.

Core Features & Use Cases

  • Guard patterns: Per-route guards, beforeEach, and beforeRouteUpdate to manage access, caching, and param changes.
  • Lifecycle awareness: Handling route param changes without re-creating components and using lifecycle hooks effectively.
  • Production readiness: Patterns for global guards, meta fields, and lazy-loaded routes to improve security and performance.

Quick Start

  1. Create a routes file with guarded routes and lazy loading for components.

Frequently Asked Questions about vue-router-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up route guards in Vue Router to control navigation?

Route guards in Vue Router intercept navigation to enforce access control and data validation. Use beforeEach for global guards, per-route guards in the route definition, or beforeRouteUpdate for handling param changes within a component. Guards receive to/from route objects and a next callback to allow, reject, or redirect navigation.

What's the best way to handle route parameter changes without recreating components?

Use beforeRouteUpdate lifecycle hook to watch param changes and update component state without triggering a remount. Combine with watchers on $route.params to sync data when users navigate between routes with the same component. This pattern improves performance and preserves component state across param updates.

How do I implement lazy loading for routes in Vue 3 SPAs?

Define route components using dynamic imports with webpack's code-splitting syntax: component: () => import('./views/Page.vue'). This defers component loading until the route is accessed, reducing initial bundle size and improving SPA performance. Pair with global guards to preload critical routes when needed.

Can I use route meta fields to manage permissions and security in Vue Router?

Route meta fields store arbitrary metadata like requiresAuth or roles on each route definition. Access meta in beforeEach guards to implement permission checks, conditional redirects, and access control logic. This approach centralizes security rules and separates concerns from component code.

What are common pitfalls when using lifecycle hooks with route navigation?

Avoid re-fetching data on every param change by using beforeRouteUpdate instead of mounted. Watch $route carefully to prevent infinite loops. Clean up async operations in beforeRouteLeave to prevent memory leaks. Ensure guards resolve or reject to avoid hanging navigation.

How do I redirect users based on authentication state in a Vue 3 SPA?

Implement global beforeEach guard logic that checks auth status via meta fields or a store. Redirect unauthenticated users to login before accessing protected routes, or redirect authenticated users away from auth pages. Use next() with a target route object to perform conditional redirects during navigation.