Overview
UI-Router’s plugin architecture allows you to extend and customize router behavior using the public API. Plugins are classes or factory functions that receive the UIRouter instance and can hook into any aspect of the routing lifecycle.UIRouterPlugin Interface
All plugins must implement theUIRouterPlugin interface:
Required Properties
name: A unique identifier for your plugin. Used to retrieve the plugin instance later viarouter.getPlugin(name).
Required Methods
dispose(router): Called when the router is disposed. Clean up event listeners, timers, or other resources here.
Creating a Plugin
Plugins can be implemented as ES6 classes, constructor functions, or factory functions.ES6 Class Plugin
Factory Function Plugin
Registering Plugins
Use therouter.plugin() method to register your plugin:
Plugin Registration Internals
TheUIRouter.plugin() method (from src/router.ts:188):
Key behaviors:
- Instantiates the plugin with the router instance and options
- Validates that the plugin has a
nameproperty - Registers the plugin as a disposable resource
- Stores the plugin in the internal registry
- Returns the plugin instance
Retrieving Plugins
Get a registered plugin by name:UIRouterPluginBase
For convenience, extend the base class which provides default implementations:Plugin Use Cases
Transition Analytics
State Loading Indicator
Custom URL Matching
Accessing Router Services
Plugins have full access to all router services:Best Practices
- Always provide a unique
name- Required for plugin registration - Implement
dispose()properly - Clean up all resources to prevent memory leaks - Store deregistration functions - Keep references to hook deregistration functions
- Validate options - Check that required options are provided in the constructor
- Use TypeScript - Leverage type safety for better plugin development
- Document your plugin - Provide clear documentation for configuration options
- Make plugins reusable - Design plugins to work across different applications