项目作者: blikblum

项目描述 :
Powerful and flexible client side router
高级语言: JavaScript
项目地址: git://github.com/blikblum/slick-router.git
创建时间: 2019-05-15T20:13:02Z
项目社区:https://github.com/blikblum/slick-router

开源协议:MIT License

下载


Slick Router

Slick Router is a powerful, flexible router that translates URL changes into route transitions allowing to put the application into a desired state. It is derived from cherrytree library (see differences).

Features

  • Out of the box support for Web Components:
    • Streamlined support for code spliting and lazy loading
    • Expose route state (query, params) to components
    • Property hooks to map global state to component props
    • Declarative handling of router links
  • Can nest routes allowing to create nested UI and/or state
  • Route transition is a first class citizen - abort, pause, resume, retry
  • Generate links in a systematic way, e.g. router.generate('commit', {sha: '1e2760'})
  • Use pushState or hashchange for URL change detection
  • Define path dynamic segments
  • Trigger router navigate programatically
  • With builtin middlewares/components:
    • components/animated-outlet: enable animation on route transitions
    • components/router-links: handle route related links state
    • middlewares/wc: advanced Web Component rendering and lifecycle (included in default export)
    • middlewares/router-links: handle route related links state (included in default export)
    • middlewares/events: fires route events on window

Installation

The default export (including web component support and routerLinks) is 17kb. The core Router class is ~12kB minified (without gzip compression). AnimatedOutlet web component, which can be used independent from the router, has a 2.5kb size.
See webpack test project for more results.

  1. $ npm install --save slick-router

Docs

Builtin middlewares

  • wc (advanced Web Component rendering and lifecycle)
  • routerLinks (handle route related links state)
  • events (fires route events on window)

Builtin components

Usage

With Web Components

The default Router class comes with Web Components and router links support.

  1. import { Router } from 'slick-router'
  2. function checkAuth(transition) {
  3. if (!!localStorage.getItem('token')) {
  4. transition.redirectTo('login')
  5. }
  6. }
  7. // route tree definition
  8. const routes = function (route) {
  9. route('application', { path: '/', component: 'my-app' }, function () {
  10. route('feed', { path: '' })
  11. route('messages')
  12. route('status', { path: ':user/status/:id' })
  13. route('profile', { path: ':user', beforeEnter: checkAuth }, function () {
  14. route('profile.lists', { component: 'profile-lists' })
  15. route('profile.edit', { component: 'profile-edit' })
  16. })
  17. })
  18. }
  19. // create the router
  20. var router = new Router({
  21. routes,
  22. })
  23. // start listening to URL changes
  24. router.listen()

Custom middlewares

Is possible to create a router with customized behavior by using the core Router with middlewares.

Check how to create middlewares in the Router Configuration Guide.

  1. import { Router } from 'slick-router/core.js'
  2. // create a router similar to page.js - https://github.com/visionmedia/page.js
  3. const user = {
  4. list() {},
  5. async load() {},
  6. show() {},
  7. edit() {},
  8. }
  9. const routes = [
  10. {
  11. name: 'users',
  12. path: '/',
  13. handler: user.list,
  14. },
  15. {
  16. name: 'user.show',
  17. path: '/user/:id/edit',
  18. handler: [user.load, user.show],
  19. },
  20. ,
  21. {
  22. name: 'user.edit',
  23. path: '/user/:id/edit',
  24. handler: [user.load, user.edit],
  25. },
  26. ]
  27. const router = new Router({ routes })
  28. function normalizeHandlers(handlers) {
  29. return Array.isArray(handlers) ? handlers : [handlers]
  30. }
  31. router.use(async function (transition) {
  32. for (const route of transition.routes) {
  33. const handlers = normalizeHandlers(route.options.handler)
  34. for (const handler of handlers) {
  35. await handler(transition)
  36. }
  37. }
  38. })
  39. // protect private routes
  40. router.use(function privateHandler(transition) {
  41. if (transition.routes.some((route) => route.options.private)) {
  42. if (!userLogged()) {
  43. transition.cancel()
  44. }
  45. }
  46. })
  47. // for error logging attach a catch handler to transition promise...
  48. router.use(function errorHandler(transition) {
  49. transition.catch(function (err) {
  50. if (err.type !== 'TransitionCancelled' && err.type !== 'TransitionRedirected') {
  51. console.error(err.stack)
  52. }
  53. })
  54. })
  55. // ...or use the error hook
  56. router.use({
  57. error: function (transition, err) {
  58. if (err.type !== 'TransitionCancelled' && err.type !== 'TransitionRedirected') {
  59. console.error(err.stack)
  60. }
  61. },
  62. })
  63. // start listening to URL changes
  64. router.listen()

Examples

You can also clone this repo and run the examples locally:

Browser Support

Slick Router works in all modern browsers. No IE support.


Copyright (c) 2024 Luiz Américo Pereira Câmara

Copyright (c) 2017 Karolis Narkevicius