router5
  • Read Me
  • Introduction
    • Why router5?
    • Getting Started
    • Ecosystem
    • Core concepts
    • Transition phase
  • Guides
    • Defining routes
    • Path Syntax
    • Router options
    • Navigating
    • In the browser
    • Observing state
  • Integration
    • With React
    • With Redux
  • Advanced
    • Plugins
    • Middleware
    • Preventing navigation
    • Errors and redirections
    • Dependency injection
    • Loading async data
    • Universal routing
    • Listeners plugin
  • API Reference
  • Migration
    • Migrating from 7.x to 8.x
    • Migrating from 6.x to 7.x
    • Migrating from 5.x to 6.x
    • Migrating from 4.x to 5.x
    • Migrating from 3.x to 4.x
    • Migrating from 2.x to 3.x
    • Migrating from 1.x to 2.x
    • Migrating from 0.x to 1.x
Powered by GitBook
On this page
  • Core API
  • createRouter
  • cloneRouter
  • add
  • start
  • isStarted
  • navigate
  • navigateToDefault
  • Stop
  • cancel
  • forward
  • getState
  • getOptions
  • setOption
  • setDependency
  • setDependencies
  • getDependencies
  • useMiddleware
  • clearMiddleware
  • usePlugin
  • canActivate
  • canDeactivate
  • clearCanDeactivate
  • buildPath
  • matchPath
  • setRootPath
  • isActive
  • areStateEqual
  • areStatesDescendants
  • Browser plugin
  • buildUrl
  • matchUrl
  • replaceHistoryState

API Reference

PreviousListeners pluginNextMigration

Last updated 6 years ago

Core API

createRouter

Create a router instance

const router = createRouter([routes], [options], [dependencies])
  • routes: your application routes, see

  • options: your router options, see

  • dependencies: the dependencies you want to make available in middleware and plugins, see

cloneRouter

Clone an existing router.

const clonedRouter = cloneRouter(router, dependencies)
  • router: the router instance to clone

  • dependencies: the new dependencies, for the cloned router (optional)

add

Add routes, see

router.add(routes)

start

router.start(startPathOrState, [done])
  • startPathOrState: a starting path (string) or state (object). When using browserPlugin, this argument is optional: path will be read from the document location

  • done: a done callback (done(err, state))

isStarted

Check if the router is in a started state

router.isStarted()

navigate

Navigate to a new route

router.navigate(routeName, [routeParams], [options], [done])
  • routeName: the name of the route to navigate to

  • routeParams: the route params

  • options: options for the transition (replace, reload, skipTransition, force or any custom option)

  • done: a done callback (done(err, state))

navigateToDefault

Navigate to the default route (if any)

router.navigateToDefault([opts], [done])

Stop

Stop your router

router.stop()

cancel

Cancel the current transition (if any)

router.cancel()

forward

Set a route to forward to another route (when navigating to the first one)

router.forward(fromRoute, toRoute)

getState

Return the current state

const state = router.getState()

getOptions

Return current options

router.getOptions()

setOption

Set an option

router.setOption(name, value)

setDependency

Set a dependency

router.setDependency(dependencyName, dependency)

setDependencies

Set dependencies

router.setDependencies(dependencies)

getDependencies

Return the current dependencies

const dependencies = router.getDependencies()

useMiddleware

const remove = router.useMiddleware(...middlewares)

clearMiddleware

Remove all middleware

router.clearMiddleware()

usePlugin

const teardown = router.usePlugin(...plugins)

canActivate

router.canActivate(name, canActivateHandler)

canDeactivate

router.canDeactivate(name, canDeactivateHandler)

clearCanDeactivate

Remove a canDeactivate handler for the provided route name

router.clearCanDeactivate(name)

buildPath

Build a path given a route name and params

router.buildPath(route, params)

matchPath

Attempt to match a path

router.matchPath(path, [source])

setRootPath

Set the root path

router.setRootPath(rootPath)

isActive

Check if the provided route is currently active

router.isActive(name, params, [strictEquality], [ignoreQueryParams])
  • name: the route name

  • params: the route params

  • strictEquality: whether to check if the given route is the active route, or a descendant of the active route (false by default)

  • ignoreQueryParams: whether to ignore query params (true by default)

areStateEqual

Compare two route state objects

router.areStatesEqual(state1, state2, ignoreQueryParams)

areStatesDescendants

Check if a state is a descendant of another state

router.areStatesDescendants(parentState, childState)

Browser plugin

The browser plugin adds the following to your router instance:

buildUrl

Build an URL

router.buildUrl(routeName, routeParams)

matchUrl

Match an URL

router.matchUrl(url)

replaceHistoryState

Replace state in history and silently update your router instance state. Use if you know what you are doing.

router.replaceHistoryState(name, params)

Register one or multiple middlewares, see

Register one or multiple plugins, see

Set a canActivate handler for the provided route name, see

Set a canDeactivate handler for the provided route name, see

See

defining routes
router options
dependency injection
defining routes
middleware
plugins
preventing navigation
preventing navigation
in the browser