ES6 Router
A simple client side router built in ES6 with 0 dependencies and TypeScript definitions.
Usage
const router = new Router({ ... })
.add(() => {
// getPage('/');
})
.add(/about/, () => {
// getPage('about');
})
.add('contact', () => {
// getPage('contact');
});
router.remove('contact');
router.navigate('about');
API
constuctor
Param | Type | Description |
---|---|---|
options | Object |
Options object |
options.debug |
boolean (false) |
Enable debugging |
options.context |
Object (window) |
Context to add event listener to |
options.startListening |
boolean (true) |
Start listening when router is created |
string
currentRoute ⇒ Name of the current route
Returns: string
- Current route
Router
add(route, handler) ⇒ Add a new route
Param | Type | Description |
---|---|---|
re |
string | RegExp
|
Name of route to match |
handler | function |
Method to execute when route matches |
Returns: Router
- This router instance
Router
remove(route, [handler]) ⇒ Remove a route from the routerc
Param | Type | Description |
---|---|---|
re |
string | RegExp
|
Name of route to remove |
[handler] | function |
Function handler to remove |
Returns: Router
- This router instance
Router
reload() ⇒ Reload the current route
Returns: Router
- This router instance
Router
listen([instance]) ⇒ Start listening for hash changes on the window
Param | Type | Default | Description |
---|---|---|---|
[instance] | any |
Window |
Context to start listening on |
Returns: Router
- This router instance
Router
stopListen([instance]) ⇒ Stop listening for hash changes on the window
Param | Type | Default | Description |
---|---|---|---|
[instance] | any |
Window |
Context to stop listening on |
Returns: Router
- This router instance
Router
navigate(path) ⇒ Navigate router to path
Param | Type | Description |
---|---|---|
path | string |
Path to navigate the router to |
Returns: Router
- This router instance
Router
navigateError(hash) ⇒ Navigate to the error page
Param | Type |
---|---|
hash | string |
Returns: Router
- This router instance
string
cleanPath(path) ⇒ Strip the path of slashes and hashes
Param | Type | Description |
---|---|---|
path | string |
Path to clean of hashes |
Returns: string
- Cleaned path
string
parseRoute(path) ⇒ Parse a route URL to get all parts
Param | Type | Description |
---|---|---|
path | string |
Route to split into parts |
Returns: string[]
- Parts of the url
License
MIT