Class: Router

Router()

Router maps [`window.history`](https://developer.mozilla.org/en-US/docs/Web/API/Window/history) events and URL path fragments (the part after the `#` in https://example.com/path#one-two-three) to events. This is usually used by an App to show and hide Components based on the URL.

Constructor

new Router()

Source:
Example

routing `/^blog\/([0-9]+)\/app\/([0-9a-z]+)$/` to an event with parameters for blog and app IDs

let router = new Router()

// Set up a couple of routes, each with a URL matching regexs and a route name

// matches http://<domain>/<path> and http://<domain>/<path>#
router.addRoute(/^$/, 'default')

// matches http://<domain>/<path>#blog/1123/app/abc-123
router.addRoute(/^blog\/([0-9]+)\/app\/([0-9a-z\-]+)$/, 'blog-app')

// Listen for the route
router.addListener('blog-app', (routeName, hash, ...regexMatches, ...parameters) => {
	// If this was an event triggered by routing to #blog/1123/app/abc-123 then:
	// `routeName` would be 'blog-app'
	// `hash` would be 'blog/1123/app/abc-123'
	// `regexMatches` would be ['1123', 'abc-123']
	// `parameters` is empty in this example but could be any number of extra items in the route (see `addRoute`)
})

// Start must be called in order to begin routing
router.start()

Members

cleanedUp :boolean

Type:
  • boolean
Source:

routes :Array.<Router>

Type:
Source:

Methods

addRoute(regex, eventName, …parameters)

Parameters:
Name Type Attributes Description
regex RegExp The regular expression that matches the incoming hash changes
eventName string The event name used when triggering
parameters * <repeatable>
Parameters passed without modification to listeners after the event name and matches
Source:

start()

This must be called in order to start routing.
Source: