Vue Router API Documentation

website·Indexed 29 days ago

https://vue-router-ru.netlify.app/

API reference for Vue Router, featuring detailed documentation on interfaces such as RouteLocationRaw, RouterLinkProps, RouteRecordSingleView, and NavigationGuardNext, as well as enumerations like ErrorTypes.

Tokens
34.5K
Snippets
181
Records
278
Agent score
75%

What's inside Vue Router

  1. Overview of Vue Router

    Vue Router is the official client-side routing solution for Vue.js. It is used in Single Page Applications (SPAs) to map browser URLs to specific Vue components, allowing the URL to update without triggering a full page reload from the server.
  2. Overview of Vue Router features

    Vue Router is the official router for Vue.js, designed to facilitate the creation of single-page applications (SPAs) through deep integration with the Vue.js core. Key capabilities include:

    • Nested route mapping
    • Dynamic routing
    • Component-based modular router configuration
    • Route parameters, query strings, and masks
    • View transition effects using the Vue.js animation system
    • Precise navigation control
    • Links with automatic active CSS classes
    • Support for both HTML5 history and hash modes
    • Customizable scrolling behavior
    • Proper URL encoding
  3. Understand the _RouteRecordBase interface

    _RouteRecordBase is an internal interface that defines the common properties for all types of RouteRecordRaw. It serves as the base for more specific route record types such as RouteRecordSingleView, RouteRecordMultipleViews, and RouteRecordRedirect.
  4. RouteLocationMatched Interface Overview

    The RouteLocationMatched interface represents a normalized version of a RouteRecord. It extends RouteRecordNormalized and is used by Vue Router to manage matched route locations, providing access to the route's configuration, components, and navigation guards.
  5. Register a leave guard with onBeforeRouteLeave

    The onBeforeRouteLeave function adds a navigation hook that triggers whenever the component corresponding to the current route is about to be left. Unlike beforeRouteLeave, this can be used in any component. The hook is automatically removed when the component is unmounted.
  6. Use the NavigationFailure interface to handle routing errors

    The NavigationFailure interface is an extended error object used by Vue Router to provide detailed information about why a navigation attempt failed. It extends the standard JavaScript Error class and includes specific routing context such as the source and destination routes, as well as the failure type.
    // Example of how a NavigationFailure might be structured
    interface NavigationFailure extends Error {
      from: RouteLocationNormalizedGeneric;
      to: RouteLocationNormalizedGeneric;
      type: NavigationFailureType;
      cause?: unknown;
      message: string;
      name: string;
      stack?: string;
    }
  7. RouterHistory Interface Overview

    The RouterHistory interface defines the implementation of history management that can be passed to a router via the Router.history option. It provides a standardized way to interact with the browser's history API, supporting both HTML5 History and other modes.
  8. Register an update guard with onBeforeRouteUpdate

    The onBeforeRouteUpdate function adds a navigation hook that triggers whenever the current route is about to be updated. Similar to beforeRouteUpdate, this can be used in any component and is automatically removed upon component unmount.
  9. Configure history modes in Vue Router 4

    The mode option has been replaced by the history option. You must now use a specific history function based on your requirements:

    • For HTML5 History mode ("history"): use createWebHistory()
    • For Hash mode ("hash"): use createWebHashHistory()
    • For Abstract/Memory mode ("abstract"): use createMemoryHistory()
    import { createRouter, createWebHistory } from 'vue-router'
    
    createRouter({
      history: createWebHistory(),
      routes: [],
    })
    
  10. Implement dynamic route transitions based on navigation depth

    You can dynamically determine the transition animation by comparing the depth of the target route versus the current route. This is achieved by using a router.afterEach hook to inject the appropriate transition name into the meta field based on the path length.
    router.afterEach((to, from) => {
      const toDepth = to.path.split('/').length
      const fromDepth = from.path.split('/').length
      to.meta.transition = toDepth < fromDepth ? 'slide-right' : 'slide-left'
    })
    <router-view v-slot="{ Component, route }">
      <transition :name="route.meta.transition">
        <component :is="Component" />
      </transition>
    </router-view>