vue-router-best-practices

Implement Vue Router navigation guards and manage dynamic route parameters.

21|1|Updated Feb 6, 2026
One-click install
npx skills add https://github.com/ShenQingchuan/locus-agent --skill vue-router-best-practices-shenqingchuan
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vue-router-best-practices
Source: https://github.com/ShenQingchuan/locus-agent/tree/main/.agents/skills/vue-router-best-practices
Command: npx skills add https://github.com/ShenQingchuan/locus-agent --skill vue-router-best-practices-shenqingchuan

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses common pitfalls and provides best practices for using Vue Router, ensuring smooth, predictable, and efficient navigation in Vue.js applications.

Core Features & Use Cases

  • Navigation Guards: Understand and implement beforeEnter, beforeEach, beforeRouteEnter, onBeforeRouteUpdate, and beforeRouteLeave correctly.
  • Route Lifecycle: Handle component re-use with dynamic route parameters and ensure proper cleanup of event listeners.
  • Use Case: Prevent users from getting stuck in infinite redirect loops, ensure data is fetched correctly when route parameters change, and properly handle asynchronous operations within navigation guards.

Quick Start

Review the documentation on handling navigation guard infinite loops.

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 prevent infinite redirect loops in Vue Router navigation guards?

Vue Router navigation guards cause infinite redirect loops when conditional logic repeatedly redirects to a guarded route. Implement explicit path checks within `beforeEach` or `beforeEnter` hooks to break the cycle and ensure predictable SPA routing.

Why does my Vue component display stale data when navigating between dynamic route parameters?

Stale data occurs in Vue components because Vue Router re-uses the same component instance for dynamic route parameter changes. Utilize `onBeforeRouteUpdate` or watch route objects to trigger data fetching and ensure proper event listener cleanup.

What is the correct way to handle asynchronous operations in Vue Router beforeRouteEnter?

Handling asynchronous operations in `beforeRouteEnter` requires using the `next` callback with a function argument, as the component instance is not yet created. This Skill explains how to safely fetch data and pass it to your component before navigation completes.

When should I use beforeRouteLeave versus global beforeEach navigation guards in Vue.js?

Use `beforeRouteLeave` for component-specific cleanup or preventing unsaved changes, and global `beforeEach` for application-wide authentication or permission checks. This Skill outlines best practices for structuring route lifecycle management in Single Page Applications.

Does Vue Router support cleanup of event listeners during route lifecycle management?

Yes, Vue Router supports event listener cleanup during route lifecycle management. By leveraging `beforeRouteLeave` or `onBeforeRouteUpdate`, you can systematically remove listeners to prevent memory leaks and ensure efficient navigation in your Vue.js application.