vue-router-best-practices

Implement Vue Router 4 navigation guards and prevent redirect loops.

2|Updated Jan 5, 2024
One-click install
npx skills add https://github.com/wilfriedago/dotfiles --skill vue-router-best-practices-wilfriedago
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vue-router-best-practices
Source: https://github.com/wilfriedago/dotfiles/tree/main/agents/skills/vue-router-best-practices
Command: npx skills add https://github.com/wilfriedago/dotfiles --skill vue-router-best-practices-wilfriedago

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 navigation, correct data handling, and preventing unexpected behavior in your Vue.js applications.

Core Features & Use Cases

  • Navigation Guards: Understand and implement beforeEnter, beforeEach, onBeforeRouteUpdate, and beforeRouteEnter correctly.
  • Route Lifecycle: Handle data fetching and component updates when navigating between routes with dynamic parameters.
  • Preventing Bugs: Avoid infinite redirect loops, stale data issues, and deprecated function usage.
  • Use Case: Ensure that a user is authenticated before accessing protected routes and that the correct data is displayed when navigating between different user profile pages.

Quick Start

Review the documentation on handling navigation guard issues with Vue Router.

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 by adding conditional checks within your global beforeEach or per-route beforeEnter guards to verify authentication status before redirecting, ensuring the target path differs from the current path to avoid cyclic routing behavior.

How do I handle stale data caused by component reuse when navigating dynamic routes?

Handle stale data from component reuse by utilizing the onBeforeRouteUpdate in-component guard, which triggers reactive data fetching and component updates when navigating between routes that share the same component but have different dynamic parameters.

What is the best way to access component instances inside Vue Router beforeRouteEnter guards?

Access component instances inside beforeRouteEnter guards by using the provided next callback function with a vm argument, allowing you to safely interact with the instantiated component and execute logic after navigation completes but before the component mounts.

Why does my Vue Router per-route guard trigger unexpectedly during SPA navigation?

Per-route beforeEnter guards trigger unexpectedly if route configurations are incorrectly nested or reused across multiple paths, requiring you to isolate route definitions and ensure the guard logic strictly evaluates the target route's metadata for proper SPA navigation control.

Can I execute asynchronous operations within Vue Router global guards?

Execute asynchronous operations within Vue Router global guards by returning a Promise or utilizing async/await syntax in your beforeEach function, ensuring navigation pauses until the asynchronous data fetching or authentication verification resolves before proceeding.

When do I need to use in-component guards versus global guards in Vue Router?

Use global beforeEach guards for application-wide concerns like authentication verification, and use in-component guards like onBeforeRouteUpdate when you need localized data fetching tied directly to specific component lifecycle interactions during dynamic parameter changes.