useRuntimeHook

源文件
在 Nuxt 应用程序中注册一个运行时钩子,并确保当作用域被销毁时它也能被正确地处理。
此可组合项在 Nuxt v3.14+ 中可用。
签名
function useRuntimeHook<THookName extends keyof RuntimeNuxtHooks> (
  name: THookName,
  fn: RuntimeNuxtHooks[THookName] extends HookCallback ? RuntimeNuxtHooks[THookName] : never
): void

使用

参数

返回

此可组合项不返回任何值,但它会在组件作用域被销毁时自动注销钩子。

示例

pages/index.vue
<script setup lang="ts">
// Register a hook that runs every time a link is prefetched, but which will be
// automatically cleaned up (and not called again) when the component is unmounted
useRuntimeHook('link:prefetch', (link) => {
  console.log('Prefetching', link)
})
</script>