Skip to content
On this page


This helper function may be used to add a listener on a hook.

When called inside a Vue component, the listener will unregister itself automatically when the component is unmounted.


function registerHook<T extends keyof Hooks>(
  hook: T,
  fn: Hooks[T],
  options?: {
    once?: boolean
): () => void

registerHook takes two arguments: the name of the hook, and a callback that takes as a parameter the data from this hook. It returns another function that will unregister the hook.

When the once option is set to true, the hook will only be executed once.


The following example will log the URL after each navigation.

registerHook('navigated', ({ payload }) => {
  console.log(`Navigated to ${payload.url}`)