API Reference | Vue Router (original) (raw)

<router-link> is the component for enabling user navigation in a router-enabled app. The target location is specified with the to prop. It renders as an <a> tag with correct href by default, but can be configured with the tag prop. In addition, the link automatically gets an active CSS class when the target route is active.

<router-link> is preferred over hard-coded <a href="..."> for the following reasons:

# v-slot API (3.1.0+)

router-link exposes a low level customization through a scoped slot (opens new window). This is a more advanced API that primarily targets library authors but can come in handy for developers as well, most of the time in a custom component like a NavLink or other.

When using the v-slot API, it is required to pass one single child to router-link. If you don't, router-link will wrap its children in a span element.

# Example: Applying Active Class to Outer Element

Sometimes we may want the active class to be applied to an outer element rather than the <a> tag itself, in that case, you can wrap that element inside a router-link and use the v-slot properties to create your link:

TIP

If you add a target="_blank" to your a element, you must omit the @click="navigate" handler.

# to

# replace

# append

# tag

# active-class

# exact

# exact-path

New in 3.5.0

# exact-path-active-class

# event

# exact-active-class

# aria-current-value

# <router-view>

The <router-view> component is a functional component that renders the matched component for the given path. Components rendered in <router-view> can also contain their own <router-view>, which will render components for nested paths.

Any non-name props will be passed along to the rendered component, however most of the time the per-route data is contained in the route's params.

Since it's just a component, it works with <transition> and <keep-alive>. When using them both together, make sure to use <keep-alive> inside:

# <router-view> Props

# name

# Router Construction Options

# routes

# mode

# base

# linkActiveClass

# linkExactActiveClass

# scrollBehavior

# parseQuery / stringifyQuery

# fallback

# Router Instance Properties

# router.app

# router.mode

# router.currentRoute

# router.START_LOCATION (3.5.0+)

# Router Instance Methods

# router.beforeEach

# router.beforeResolve

# router.afterEach

Signatures:

Add global navigation guards. See Navigation Guards for more details.

All three methods return a function that removes the registered guard/hook.

# router.push

# router.replace

# router.go

# router.back

# router.forward

Signatures:

Programmatically navigate to a new URL. See Programmatic Navigation for more details.

These functions can only be called after installing the Router plugin and passing it to the root Vue instance as shown in the Getting Started.

# router.getMatchedComponents

Signature:

Returns an Array of the components (definition/constructor, not instances) matched by the provided location or the current route. This is mostly used during server-side rendering to perform data prefetching.

# router.resolve

Signature:

Reverse URL resolving. Given location in form same as used in <router-link/>.

# router.addRoutes

DEPRECATED: use router.addRoute() instead.

Signature:

Dynamically add more routes to the router. The argument must be an Array using the same route config format with the routes constructor option.

# router.addRoute

New in 3.5.0

Add a new route to the router. If the route has a name and there is already an existing one with the same one, it overwrites it.

Signature:

# router.addRoute

New in 3.5.0

Add a new route record as the child of an existing route. If the route has a name and there is already an existing one with the same one, it overwrites it.

Signature:

# router.getRoutes

New in 3.5.0

Get the list of all the active route records. Note only documented properties are considered Public API, avoid using any other property e.g. regex as it doesn't exist on Vue Router 4.

Signature:

# router.onReady

Signature:

This method queues a callback to be called when the router has completed the initial navigation, which means it has resolved all async enter hooks and async components that are associated with the initial route.

This is useful in server-side rendering to ensure consistent output on both the server and the client.

The second argument errorCallback is only supported in 2.4+. It will be called when the initial route resolution runs into an error (e.g. failed to resolve an async component).

# router.onError

Signature:

Register a callback which will be called when an error is caught during a route navigation. Note for an error to be called, it must be one of the following scenarios:

# The Route Object

A route object represents the state of the current active route. It contains parsed information of the current URL and the route records matched by the URL.

The route object is immutable. Every successful navigation will result in a fresh route object.

The route object can be found in multiple places:

# Route Object Properties

# Component Injections

# Component Injected Properties

These properties are injected into every child component by passing the router instance to the root instance as the router option.

# Component Enabled Options