A router tool based on decorator and hash string.
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.
npm install hash-route
MIT