Skip to content

Store API ​

useRouterStore ​

ts
const store = useRouterStore<VmoRouteToRaw<Meta>>()

Call after installing Pinia. The store ID is router; state is shared within the same Pinia instance. Avoid using that ID for another application store.

Getters ​

GetterMeaning
getCachedRoutesIn-memory definitions first, otherwise the injected getter; defaults to an empty array
getKeepAliveRouteNamesKeepAlive name list
getMutipleCatchWhether to persist multiple dynamic routes; defaults to true
getBrowserBeforeunloadDisabledReload/close protection; defaults to false
getRouteToLeaveDisabledSPA leave protection; defaults to false
getKeepAliveMaxName limit; unset or 0 means unlimited
getCacheMethodCurrent persistence callbacks
getConfirmToLeaveMethodCurrent confirmation callback

Actions ​

ActionBehavior
insertCachedRoute(to)Insert or update by name and invoke the setter
removeCachedRoute(name)Remove the definition and invoke the setter; string/symbol names supported
clearDynamicRouters()Clear definitions and invoke the setter without removing registered routes
setCacheMethods({ getter, setter })Set synchronous persistence callbacks
setMutipleCatch(boolean)Change persistence mode for the next write
setKeepAliveName(string | string[])Replace the name list
insertKeepAliveName(string | string[])Insert unique names and trim to the limit
removeKeepAliveName(string | string[])Remove names from the list
setKeepAliveMax(number = 0)Round the absolute value; trim on subsequent insertion
setBrowserBeforeunloadDisabled(boolean)Set reload/close protection
setRouteToLeaveDisabled(boolean)Set SPA leave protection
setConfirmToLeaveMethod(fn)Set the confirmation callback receiving the departing page's meta

Use string route names for serializable cache definitions. Multiple routers sharing this store share name lists and protection flags; the primary usage is one router per application.

Default state ​

StateDefaultMeaning
cachedRoutesnullFalls back to storage getter or []; after writing, the memory array takes priority
cacheMethodsundefinedNo default localStorage/sessionStorage choice
mutipleCatchtrueRetain multiple definitions; false retains only the current write
keepAliveRouteNames[]Component include list, not instances
keepAliveMaxundefinedUnset or 0 means unlimited
routeToLeaveDisabledfalsetrue enables SPA leave confirmation
browserBeforeunloadDisabledfalsetrue enables refresh/close protection
confirmToLeaveMethodasync () => trueAllows leaving by default; no automatic dialog

All actions synchronously return void; the registered confirmation callback may itself return a Promise. Getters only read state and never register routes.

ActionParameter / defaultSide effects and limits
insertCachedRouteto: RouteToRawUpdate/append by name in multi mode; replace the array in single mode; always call setter
removeCachedRoutename: string or symbolFilter definitions and call setter even for missing names
clearDynamicRoutersNoneSet cachedRoutes=[] and call setter; do not clear registrations or KeepAlive
setCacheMethodsSynchronous getter/setterSet callbacks only; no immediate read, write or restore; existing memory arrays take priority
setMutipleCatchbooleanChange future writes without trimming current definitions
setKeepAliveNamestring or string[]Replace directly without deduplication or max trimming
insertKeepAliveNamestring or string[]Deduplicate then retain the last max names in insertion order; duplicates do not move
removeKeepAliveNamestring or string[]Remove names only, not routes or persisted definitions
setKeepAliveMaxnumber=0Math.round(Math.abs(max)); applies on later inserts
setRouteToLeaveDisabledbooleanSet SPA flag; successful navigation resets it to false
setBrowserBeforeunloadDisabledbooleanSet browser flag; lifecycle listener must also be installed
setConfirmToLeaveMethod(meta) => boolean or Promise<boolean>Replace callback; receives the departing page's meta

Setters should synchronously handle storage errors. Actions do not catch them and may throw after memory state changes. An empty array prevents getter fallback; account switching normally clears routes and explicitly loads new definitions. Replacing the getter alone does not automatically restore routes.

ts
store.setKeepAliveMax(5)
store.insertKeepAliveName(['report-monthly', 'report-yearly'])
store.setConfirmToLeaveMethod(async () => window.confirm('Discard unsaved changes?'))
store.setRouteToLeaveDisabled(true)
// After saving:
store.setRouteToLeaveDisabled(false)

router.removeRoute/clearRoutes synchronize multiple states. Individual store actions affect their own state only; clearDynamicRouters is not a substitute for removing registered routes.

Getter return types ​

getCachedRoutes returns RouteToRaw[]; getKeepAliveRouteNames returns string[]; getMutipleCatch, getBrowserBeforeunloadDisabled and getRouteToLeaveDisabled return boolean; getKeepAliveMax returns number | undefined; getCacheMethod returns CacherMethods<RouteToRaw> | undefined; getConfirmToLeaveMethod returns the confirmation callback or undefined.