vue-router-best-practices

Diagnose and fix Vue Router 4 navigation guard, route param, and lifecycle issues.

Updated Sep 17, 2026
One-click install
npx skills add https://github.com/bramanda48/skills --skill vue-router-best-practices-bramanda48
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vue-router-best-practices
Source: https://github.com/bramanda48/skills/tree/main/skills/vue-router-best-practices
Command: npx skills add https://github.com/bramanda48/skills --skill vue-router-best-practices-bramanda48

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Vue Router 4 has subtle behaviors that cause real bugs: per-route beforeEnter guards skip param changes, beforeRouteEnter cannot access the component instance, unawaited async guards let unauthorized navigation through, misconfigured redirects create infinite loops, and route param changes never re-trigger lifecycle hooks, leaving stale data on screen. ## Core Features & Use Cases - Navigation Guard Patterns: Correct async/await guard usage, return-based syntax replacing the deprecated next() callback, and infinite redirect loop prevention with route meta fields. - Route Param & Lifecycle Handling: Watch-based and onBeforeRouteUpdate patterns for refetching data when navigating between same-route params like /users/1 to /users/2. - Setup Guidance: When simple hash routing is acceptable versus when production SPAs require Vue Router with lazy loading, guards, and history mode. - Use Case: A user reports seeing the previous user's profile after clicking a different user link. The skill identifies the missing param watcher and provides the watch with immediate:true fix. ## Quick Start Ask the agent to review your Vue Router navigation guards and route components for common Vue Router 4 pitfalls and apply the recommended fixes.

Frequently Asked Questions about vue-router-best-practices

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

FAQPage Schema
Why doesn't my Vue component update when route params change?

Vue Router reuses the component instance when navigating between the same route with different params, so onMounted and created hooks do not fire again. Fix it by watching route.params with immediate: true or using the onBeforeRouteUpdate in-component guard to refetch data.

How do I fix infinite redirect loops in Vue Router navigation guards?

Infinite loops happen when a guard redirects to a route that triggers the same guard again. Exclude the target route from the check, use route meta fields like requiresAuth to control which routes need protection, and test direct URL access and refresh scenarios.

Why is beforeRouteEnter unable to access this in Vue Router?

The beforeRouteEnter guard runs before the component is created, so this is undefined. Use the next(vm => ...) callback to access the instance after navigation, or in Composition API fetch data in onMounted or via route meta instead.

Should I use next() or return values in Vue Router 4 guards?

Vue Router 4 deprecates the next() callback in favor of return-based syntax. Return a route to redirect, false to cancel, or nothing to proceed. If legacy code uses next(), it must be called exactly once per code path with an early return after each call.

Does beforeEnter run when only route params or query change?

No. Per-route beforeEnter guards only fire when entering from a different route, not when params, query, or hash change within the same route. Use onBeforeRouteUpdate in the component or a global beforeEach guard for param-based validation.

When should I use Vue Router instead of simple hash routing?

Simple hash routing with hashchange listeners is acceptable only for learning or tiny prototypes, and requires manual event listener cleanup on unmount. Production SPAs need Vue Router for navigation guards, nested routes, lazy loading, history mode, and scroll behavior.