Skip to content

RouterPlugin / PluginContext ​

Type reference for the plugin system: custom plugins extend RouterPlugin and register hooks through PluginContext. For usage and complete examples see Plugin System.

ts
import { RouterPlugin } from '@meng-xi/unix-router'
import type {
	PluginContext,
	PluginData,
	PluginNavigationMode,
	NavigationPrepareContext,
	NavigationCompleteContext
} from '@meng-xi/unix-router'

RouterPlugin ​

Abstract base class for plugins. Must be implemented by extending with a class and registered as an instance (on Kotlin / Swift, object literals containing methods are inferred as UTSJSONObject and cannot work as plugins):

ts
abstract class RouterPlugin {
	/** Plugin name (matched by hasPlugin(name) and the PLUGIN_REQUIRED pre-check) */
	name: string
	/** Install the plugin: register hooks via context, read plugin options from options; called once inside createRouter */
	abstract install(context: PluginContext, options: RouterOptions): void
}

PluginContext ​

The hook registration interface the router exposes to plugins (carried by a class):

Hook registration (8):

MemberSignatureTrigger point
onEnrichLocation(hook: (location: RouteLocationRaw) => RouteLocationRaw) => voidEnriches the raw location before matcher.resolve(); runs in a chain
onAfterResolve(hook: (enrichedLocation: RouteLocationRaw, pluginData: PluginData) => void) => voidAfter resolve, before the guard chain; extracts plugin data
onPrepareNavigation(hook: (ctx: NavigationPrepareContext) => void) => voidBefore the uni API call; may modify ctx.query / ctx.options
onBeforeNavigation(hook: (ctx: NavigationPrepareContext) => Promise<void> | void) => voidJust before the uni API is actually invoked; may be async, multiple hooks run serially
onCompleteNavigation(hook: (ctx: NavigationCompleteContext) => void) => voidAfter the uni API succeeds and the page stack is confirmed; may extend ctx.result
onNavigationAbort(hook: (pluginData: PluginData) => void) => voidCleans up when a navigation aborts / fails (exceptions are swallowed)
onRouteSync(hook: (query: Map<string, string>, params: Map<string, string>) => void) => voidDuring route state sync; extracts plugin data from the URL query
onAppInstall(hook: (app: any) => void) => voidFired when app.use(router) is called

Context members:

MemberTypeDescription
currentRouteRouteLocationThe current route location (read-only getter, fetched in real time)
resolve(location: RouteLocationRaw) => RouteLocationResolves a route location (equivalent to router.resolve)
routeranyReference to the router instance (avoids native-side interface degradation across files; cast with as Router inside plugins as needed)
paramsManageranyThe core's shared ParamsManager instance (cast with as ParamsManager as needed)
hasPlugin(name: string) => booleanChecks whether a given plugin is registered

PluginData ​

ts
type PluginData = Map<string, any>

The data container shared by plugin stages within a single navigation: onAfterResolve writes → onPrepareNavigation / onBeforeNavigation / onCompleteNavigation read → onNavigationAbort cleans up. A redirect reuses the same pluginData.

The context of onPrepareNavigation / onBeforeNavigation:

MemberTypeDescription
to / fromRouteLocationTarget / source route
modePluginNavigationMode'push' | 'replace' | 'relaunch' | 'back'
pluginDataPluginDataData shared across stages
queryMap<string, string>The query of the actual navigation URL (mutable: internal keys can be added)
optionsUniNavigationOptionsThe uni navigation options (mutable: animation params, etc., can be changed)

The context of onCompleteNavigation:

MemberTypeDescription
toRouteLocationThe target route
modePluginNavigationModeThe navigation mode
pluginDataPluginDataData shared across stages
resultRouteLocationThe navigation result (mutable: can be extended)

Built-in Plugins and Helper Exports ​

ExportDescriptionGuide
ParamsPlugin / createParamsManagerPage parameter passing (in-memory / persisted)Parameter Passing
EventsPlugin / eventBusPage-to-page communication (events registry + EventChannel)Page-to-Page Communication
AnimationPluginNavigation window animation (App native pass-through / H5 WAAPI)Navigation Animation
InterceptorPlugin / installInterceptors / removeInterceptorsIntercepts the uni native navigation APIsuni API Interception
  • Plugin System — architecture, registration, and a complete custom plugin example
  • RouterOptions — the plugins option and each plugin's companion options
  • Navigation Flow — the exact position of each hook in the navigation timeline

Released under the MIT License.