Navigation

After configuring your routes, you need to enable navigation by starting your router instance.

Starting your router

const myRouter = Router5([
    { name: 'home', path: '/home' },
    { name: 'about', path: '/about' },
    { name: 'contact', path: '/contact' }
]);

myRouter.start('/home');

When using .start(), you should supply a starting path or state except if you use the browser plugin (the current URL will automatically be used).

Invoking the .start(startPathOrState[, done]) function will:

And will:

Providing a starting state is designed to be used for universal JavaScript applications: see universal applications.

Defining a default route

A default route can be set in Router5 constructor options. The following example will cause your application to navigate to /about:

var myRouter = Router5([
        { name: 'home', path: '/home' },
        { name: 'section', path: '/:section' }
    ], {
        defaultRoute: 'section'
        defaultParams: {section: 'about'}
    })
    .start(function (err, state) {
        /* ... */
    });

A callback can be passed to start and will be invoked once the router has transitioned to the default route.

Router5 exposes the following method: navigate(routeName, routeParams, opts). This method has to be called for navigating to a different route: clicks on links won't be intercepted by the router.

myRouter.navigate('section', {section: 'contact'});
// Will navigate to '/contact'

Forcing a reload

When trying to navigate to the current route nothing will happen unless reload is set to true.

myRouter.navigate('section', {section: 'contact'}, {reload: true});

Replacing current state

Set replace to true for replacing the current state in history when navigating to a new route. Default behaviour is to add an entry in history.

myRouter.navigate('section', {section: 'contact'}, {replace: true});

Knowing when a transition has been successful or not

Like for .start(), .navigate() accepts a callback (last argument):

myRouter.navigate('route', function (err, state) {
    /* ... */
})

Stopping your router

At any time you can stop (pause) a router and it will prevent any navigation. To resume, simply invoke .start() again.

myRouter.stop();