Migrating from 1.x to 2.x

New features

  • You can now pass to router.add() objects containing canActivate functions and those functions will be registered. No need to call for each route addNode or canActivate.
  • Persistent parameters plugin now available.

Breaking change

  • router.registerComponent and router.deregisterComponent have been removed in favour of canDeactivate
  • Additional arguments now apply to middleware functions.
  • Context has been removed from middleware functions.
  • Middleware functions are now a function taking a router instance and returning a function called on each transition.
  • Plugins, like middleware functions, are now a function taking a router instance and returning an object of methods (which doesn't contain an init function anymore).
  • autoCleanUp is no longer shared with router5-listeners. If you need to turn off automatic deregistration of node listeners, pass { autoCleanUp: false } to the listeners plugin.
  • router5 package now exports Router5 as default, and RouteNode, loggerPlugin, errCodes and transitionPath as named exports

Code example

ES2015+
1
import Router5, { loggerPlugin } from 'router5';
2
import historyPlugin from 'router5-history';
3
import listenersPlugin from 'router5-listeners';
4
5
const router = new Router5()
6
.add([{
7
name: 'home',
8
path: '/home'
9
}])
10
.usePlugin(historyPlugin())
11
.usePlugin(listenersPlugin())
12
// Development helper
13
.usePlugin(loggerPlugin())
14
.start();
Copied!
ES5
1
var router5 = require('router5');
2
var Router5 = router5.default;
3
var loggerPlugin = router5.loggerPlugin;
4
5
var historyPlugin = require('router5-history');
6
var listenersPlugin = require('router5-listeners');
7
8
var router = new Router5()
9
.add([{
10
name: 'home',
11
path: '/home'
12
}])
13
.usePlugin(historyPlugin())
14
.usePlugin(listenersPlugin())
15
// Development helper
16
.usePlugin(loggerPlugin())
17
.start();
Copied!