Skip to content

🔀 A simple frontend router based on decorator and hash string

License

Notifications You must be signed in to change notification settings

kt3k/hash-route

Repository files navigation

hash-route v2.0.3

CircleCI codecov

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

About

🔀 A simple frontend router based on decorator and hash string

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •