hash-route

2.0.3 • Public • Published

hash-route v2.0.3

CircleCI codecov Greenkeeper badge

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

Package Sidebar

Install

npm i hash-route

Weekly Downloads

1

Version

2.0.3

License

MIT

Unpacked Size

184 kB

Total Files

10

Last publish

Collaborators

  • kt3k