Plugins
router5 is extensible with the use of plugins. Plugins can decorate a route instance and do things on specific router and transition events.

Plugin requirements

A plugin is a function taking a router instance and returning an object with a name and at least one of the following methods:
  • onStart(): invoked when router.start() is called
  • onStop(): invoked when router.stop() is called
  • onTransitionStart(toState, fromState)
  • onTransitionCancel(toState, fromState)
  • onTransitionError(toState, fromState, err)
  • onTransitionSuccess(toState, fromState, opts) (options contains replace and reload boolean flags)
  • teardown(): a function called when removing the plugin

Registering a plugin

1
function myPlugin(router, dependencies) {
2
return {
3
onTransitionSuccess: (toState, fromState) => {
4
console.log(
5
'Yippee, navigation to ' + toState.name + ' was successful!'
6
)
7
}
8
}
9
}
10
11
const router = createRouter()
12
13
router.usePlugin(myPlugin)
Copied!

Plugin examples

Router5 includes a logging plugin that you can use to help development
1
import createRouter, { loggerPlugin } from 'router5'
2
3
const router = createRouter()
4
5
const teardownPlgin = router.usePlugin(loggerPlugin)
Copied!
Last modified 3yr ago