vue-router-best-practices

Document Vue Router 4 best practices for guards and data loading.

Updated Dec 8, 2025
One-click install
npx skills add https://github.com/sduduzog/.dotfiles --skill vue-router-best-practices-sduduzog
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vue-router-best-practices
Source: https://github.com/sduduzog/.dotfiles/tree/main/agents/skills/vue-router-best-practices
Command: npx skills add https://github.com/sduduzog/.dotfiles --skill vue-router-best-practices-sduduzog

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Vue Router is central to building scalable Vue apps. This guide consolidates best practices for guards, navigation flows, and lifecycle interactions to prevent common pitfalls.

Core Features & Use Cases

  • Per-route guards: beforeEnter for route-specific validation and redirection.
  • In-component guards: onBeforeRouteUpdate and beforeRouteEnter for param and data changes.
  • Global guards and patterns: beforeEach, afterEach, and error handling to enforce auth and data loading.
  • Safe handling of async navigation and avoiding infinite redirect loops with meta-based and guard-composition strategies.

Quick Start

Show a minimal Vue Router guard setup for protecting a /dashboard route.

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?

Prevent infinite redirect loops in Vue Router navigation guards by applying meta-based strategies and guard-composition patterns. These practices ensure conditional logic terminates correctly, enforcing auth checks without cycling between protected routes.

What is the best way to use beforeEnter for route-specific validation in Vue Router 4?

Use beforeEnter for route-specific validation in Vue Router 4 by configuring per-route guards. This practice isolates validation logic and redirection handling directly within route definitions, keeping global guards clean.

How do I handle async data loading with in-component guards like beforeRouteUpdate?

Handle async data loading with in-component guards like beforeRouteUpdate by managing param changes within the component lifecycle. This practice ensures safe data fetching and resolves navigation only after required asynchronous operations complete.

Does this Vue Router best practices guide apply to Vue 3 SPAs?

This Vue Router best practices guide applies directly to Vue 3 SPAs using Vue Router 4. It covers real-world routing scenarios including global beforeEach guards, afterEach error handling, and safe in-component data fetching.

When should I use global beforeEach versus in-component guards for auth checks?

Use global beforeEach guards to enforce application-wide auth and data loading rules. In-component guards like onBeforeRouteUpdate are for handling specific param changes and data interactions within that component's lifecycle.

Why does beforeRouteUpdate fail to trigger data fetching on param changes?

beforeRouteUpdate fails to trigger data fetching on param changes when the component instance is reused instead of recreated. Implement safe data fetching patterns within the in-component guard to detect and load new data dynamically.