After installation, import and create a UIRouter instance:
1
Import UIRouter
Import the UIRouter class from the package:
import { UIRouter } from '@uirouter/core';
2
Create router instance
Instantiate the UIRouter class:
const router = new UIRouter();
The basic UIRouter constructor creates a router without location services. For browser-based routing, you’ll need to add a location plugin (see next step).
3
Add location plugin
For browser-based applications, install a location service plugin:
import { UIRouter } from '@uirouter/core';import { pushStateLocationPlugin } from '@uirouter/core';const router = new UIRouter();router.plugin(pushStateLocationPlugin);
Stores location in memory without modifying the browser URL. Useful for testing or non-browser environments:
import { UIRouter } from '@uirouter/core';import { memoryLocationPlugin } from '@uirouter/core';const router = new UIRouter();router.plugin(memoryLocationPlugin);// No URL changes, routing state in memory only
For vanilla JavaScript applications, you may also want to include the services plugin, which provides basic dependency injection and promise support:
import { UIRouter } from '@uirouter/core';import { servicesPlugin, pushStateLocationPlugin } from '@uirouter/core';const router = new UIRouter();router.plugin(servicesPlugin);router.plugin(pushStateLocationPlugin);
The services plugin provides $q (promise implementation) and $injector (dependency injection) for vanilla JavaScript. Framework-specific implementations typically provide their own services.