vue-router-best-practices

Guide Vue Router navigation guards and route lifecycle handling in Vue.js.

Updated Jan 22, 2026
One-click install
npx skills add https://github.com/cooker/wx-mp-article --skill vue-router-best-practices-cooker
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vue-router-best-practices
Source: https://github.com/cooker/wx-mp-article/tree/main/.cursor/skills/vue-router-best-practices
Command: npx skills add https://github.com/cooker/wx-mp-article --skill vue-router-best-practices-cooker

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps developers avoid common pitfalls and implement robust navigation patterns in Vue.js applications using Vue Router, ensuring smooth user experiences and predictable application behavior.

Core Features & Use Cases

  • Navigation Guards: Understand and implement beforeEnter, beforeEach, beforeRouteUpdate, and beforeRouteLeave guards effectively.
  • Route Lifecycle: Manage component lifecycle hooks and data fetching when navigating between routes, especially when route parameters change.
  • Use Case: Prevent users from accessing protected routes without authentication, handle unsaved changes before leaving a route, or ensure data is fresh when navigating between different user profiles.

Quick Start

Explain how to use the onBeforeRouteUpdate guard to fetch fresh data when route parameters change.

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 use Vue Router navigation guards to prevent access to protected routes?

Vue Router navigation guards prevent unauthorized access to protected routes by conditionally redirecting users in hooks like `beforeEach` or `beforeEnter`. You check authentication state and stop navigation if access is unauthorized.

How do I fetch fresh data when Vue Router parameters change in a SPA?

To fetch fresh data when Vue Router parameters change, use the `onBeforeRouteUpdate` guard. This reacts to dynamic parameter updates within the same component, preventing stale data issues during SPA routing.

How does Vue Router interact with the Vue.js component lifecycle during route transitions?

Vue Router interacts with the Vue.js component lifecycle by triggering route hooks like `beforeRouteUpdate` alongside standard lifecycle hooks. Managing this ensures predictable behavior when components are reused or destroyed during navigation.

What is the best way to handle unsaved changes before leaving a route in Vue Router?

The best way to handle unsaved changes before leaving a route in Vue Router is the `beforeRouteLeave` guard. It allows you to warn users or cancel navigation, ensuring unsaved data is not accidentally lost.

Why does my Vue Router navigation guard cause an infinite redirect loop?

Vue Router navigation guards cause infinite redirect loops when redirect logic lacks a proper exit condition. Best practices require verifying target route and authentication status before redirecting to avoid cyclic `beforeEach` calls.

What do I need to know to implement robust SPA routing with Vue Router?

Implementing robust SPA routing with Vue Router requires understanding the Vue Router API and Vue.js component lifecycle. You need knowledge of navigation guards and data fetching patterns to ensure predictable application behavior.