History managers
Or using the registerRouterInjector
helper function:
import Registry from '@dojo/framework/core/Registry';
import { registerRouterInjector } from '@dojo/framework/routing/RouterInjector';
import StateHistory from '@dojo/framework/routing/history/StateHistory';
import routes from './routes';
const registry = new Registry();
registerRouterInjector(routes, registry);
// creates and registers a router using the `StateHistory`
registerRouterInjector(routes, registry, { HistoryManager: StateHistory });
HashHistory
uses the fragment identifier to process route changes, for example https://foo.com/#home
would process the home
as the route path. As HashHistory
is the default manager, you do not need to import the module.
StateHistory
uses the browser’s history API, to manage application route changes.
- Re-writing the
index.html
request to load from the application root. - Re-writing requests to load static resources (
.js
,.css
etc) from the application root.
Note: This machinery is included with @dojo/cli-build-app
using the option (intended for development only).
import { Router } from '@dojo/framework/routing/Router';
import { StateHistory } from '@dojo/framework/routing/history/StateHistory';
const router = new Router(config, { HistoryManager: StateHistory });
The MemoryHistory
does not rely on any browser API but keeps its own internal path state. It should not be used in production applications but is useful for testing application routing.
import renderer from '@dojo/framework/core/vdom';
import { registerRouterInjector } from '@dojo/framework/routing/RouterInjector';
import routes from './routes';
import App from './App';
const registry = new Registry();
// creates a router with the routes and registers the router with the registry
registerRouterInjector(routes, registry);
r.mount({ registry });
These history managers work like adapters, meaning that custom history managers can be implemented by fulfilling the history manager interface.