hash-route
v2.0.3
Published
A router based on hash string.
Downloads
9
Maintainers
Readme
hash-route v2.0.3
A router tool based on decorator and hash string.
Usage
You can register the route by @route
decorator.
Note: You can put string literal at the method name position in es2015.
const {route, dispatch} = require('hash-route')
class Router {
constructor () {
$(window).on('hashchange', () => dispatch(this))
}
@route '#' () {
location.replace('#projects')
}
@route '#projects' () {
showAllProjectPage()
}
@route '#projects/:project' (params) {
showProjectPage(params.project)
}
@route '*' () {
showNotFoundPage()
}
...
}
With the above example, each time the hash string changed the registered routes are compared with location.hash
and if it matches, the corresponding route (method) is invoked.
The express style patterns are available as route paths. This library uses path-to-regexp under the hood. See its document for available patterns.
Install
npm install hash-route
License
MIT