function useRuntimeHook<THookName extends keyof RuntimeNuxtHooks> (
name: THookName,
fn: RuntimeNuxtHooks[THookName] extends HookCallback ? RuntimeNuxtHooks[THookName] : never
): void
name: 要注册的运行时钩子的名称。你可以在此处查看完整的 Nuxt 运行时钩子列表。fn: 当钩子被触发时执行的回调函数。函数签名会根据钩子名称而异。此可组合项不返回任何值,但它会在组件作用域被销毁时自动注销钩子。
<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>