editLink |
---|
API Documentation / Router
Router instance.
• Readonly
currentRoute: Ref
<RouteLocationNormalizedLoaded
>
Current RouteLocationNormalized
• listening: boolean
Allows turning off the listening of history events. This is a low level api for micro-frontends.
• Readonly
options: RouterOptions
Original options object passed to create the Router
▸ addRoute(parentName
, route
): () => void
Add a new route record as the child of an existing route.
Name | Type | Description |
---|---|---|
parentName |
RouteRecordName |
Parent Route Record where route should be appended at |
route |
RouteRecordRaw |
Route Record to add |
fn
▸ (): void
void
▸ addRoute(route
): () => void
Add a new route record to the router.
Name | Type | Description |
---|---|---|
route |
RouteRecordRaw |
Route Record to add |
fn
▸ (): void
void
▸ afterEach(guard
): () => void
Add a navigation hook that is executed after every navigation. Returns a function that removes the registered hook.
Name | Type | Description |
---|---|---|
guard |
NavigationHookAfter |
navigation hook to add |
fn
a function that removes the registered hook
▸ (): void
void
Example
router.afterEach((to, from, failure) => {
if (isNavigationFailure(failure)) {
console.log('failed navigation', failure)
}
})
▸ back(): void
Go back in history if possible by calling history.back()
. Equivalent to
router.go(-1)
.
void
▸ beforeEach(guard
): () => void
Add a navigation guard that executes before any navigation. Returns a function that removes the registered guard.
Name | Type | Description |
---|---|---|
guard |
NavigationGuardWithThis <undefined > |
navigation guard to add |
fn
▸ (): void
void
▸ beforeResolve(guard
): () => void
Add a navigation guard that executes before navigation is about to be resolved. At this state all component have been fetched and other navigation guards have been successful. Returns a function that removes the registered guard.
Name | Type | Description |
---|---|---|
guard |
NavigationGuardWithThis <undefined > |
navigation guard to add |
fn
a function that removes the registered guard
▸ (): void
void
Example
router.beforeResolve(to => {
if (to.meta.requiresAuth && !isAuthenticated) return false
})
▸ forward(): void
Go forward in history if possible by calling history.forward()
.
Equivalent to router.go(1)
.
void
▸ getRoutes(): RouteRecordNormalized
[]
Get a full list of all the route records.
▸ go(delta
): void
Allows you to move forward or backward through the history. Calls
history.go()
.
Name | Type | Description |
---|---|---|
delta |
number |
The position in the history to which you want to move, relative to the current page |
void
▸ hasRoute(name
): boolean
Checks if a route with a given name exists
Name | Type | Description |
---|---|---|
name |
RouteRecordName |
Name of the route to check |
boolean
▸ isReady(): Promise
<void
>
Returns a Promise that resolves 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. If the initial navigation already happened, the promise resolves immediately.
This is useful in server-side rendering to ensure consistent output on both the server and the client. Note that on server side, you need to manually push the initial location while on client side, the router automatically picks it up from the URL.
Promise
<void
>
▸ onError(handler
): () => void
Adds an error handler that is called every time a non caught error happens
during navigation. This includes errors thrown synchronously and
asynchronously, errors returned or passed to next
in any navigation
guard, and errors occurred when trying to resolve an async component that
is required to render a route.
Name | Type | Description |
---|---|---|
handler |
_ErrorListener |
error handler to register |
fn
▸ (): void
void
▸ push(to
): Promise
<undefined
| void
| NavigationFailure
>
Programmatically navigate to a new URL by pushing an entry in the history stack.
Name | Type | Description |
---|---|---|
to |
RouteLocationRaw |
Route location to navigate to |
Promise
<undefined
| void
| NavigationFailure
>
▸ removeRoute(name
): void
Remove an existing route by its name.
Name | Type | Description |
---|---|---|
name |
RouteRecordName |
Name of the route to remove |
void
▸ replace(to
): Promise
<undefined
| void
| NavigationFailure
>
Programmatically navigate to a new URL by replacing the current entry in the history stack.
Name | Type | Description |
---|---|---|
to |
RouteLocationRaw |
Route location to navigate to |
Promise
<undefined
| void
| NavigationFailure
>
▸ resolve(to
, currentLocation?
): RouteLocation
& { href
: string
}
Returns the normalized version of a
route location. Also includes an href
property
that includes any existing base
. By default, the currentLocation
used is
router.currentRoute
and should only be overridden in advanced use cases.
Name | Type | Description |
---|---|---|
to |
RouteLocationRaw |
Raw route location to resolve |
currentLocation? |
RouteLocationNormalizedLoaded |
Optional current location to resolve against |
RouteLocation
& { href
: string
}