API Reference

September 21, 2020 ยท View on GitHub

Table of Contents

<RouterView />

Renders the routes. No additional props.

PropTypeDefaultDescription
tostring | LocationInputTarget route
replacebooleanfalseReplace current route instead of pushing
  • Renders an anchor tag
  • Adds aria-current="page" when link exactly matches
  • Display correct href with resolved dynamic segments, base path, and hash prepends, e.g. /user/:id => #/user/foo
  • Adds active class names based on to and current route path:
    • link-active when link partially matches, e.g. /foo matches /foo/bar
    • link-exact-active when link exactly matches, e.g. /foo matches /foo

initHashRouter(routes: RouteRecord[])

Initializes a hash mode router for the app.

initPathRouter(routes: RouteRecord[])

Initializes a path mode router for the app.

navigate() has two function signatures:

Navigate using an offset in the current history. Works the same way as history.go.

Navigate to a route using a path string or a location descriptor object. string and LocationInput are semantically equal and are just different ways to express the same route. For example:

'/foo?key=value#top'

// same as

{
  path: '/foo',
  search: { key: 'value' },
  hash: '#top'
}

$route

A readable store that contains the current route information.

PropertyTypeExampleDescription
pathstring'/user/foo'The route path without search and hash
paramsRecord<string, string>{ id: 'foo' }The parsed dynamic segments of the path, e.g. /user/:id
searchURLSearchParamsThe path search parsed with URLSearchParams
hashstring'#hey'The path hash with leading #. Empty string if no hash.
matchedRouteRecord[]The array of route records that matches the current route

createLink(to: string | LocationInput)

Returns a readable store that contains the given link's information.

PropertyTypeExampleDescription
hrefstring'#/user/foo'The path with resolved dynamic segments, base path, and hash prepends
isActivebooleanfalseWhether the link is partially matching the current path
isExactActivebooleanfalseWhether the link is exactly matching the current path