useRuntimeHook

原始檔
在 Nuxt 應用程式中註冊一個執行時鉤子,並確保在作用域銷燬時正確地處理它。
此可組合項在 Nuxt v3.14+ 中可用。
簽名
function useRuntimeHook<THookName extends keyof RuntimeNuxtHooks> (
  name: THookName,
  fn: RuntimeNuxtHooks[THookName] extends HookCallback ? RuntimeNuxtHooks[THookName] : never
): void

使用

引數

  • name:要註冊的執行時鉤子的名稱。你可以在此處檢視完整的執行時 Nuxt 鉤子列表
  • fn:鉤子觸發時要執行的回撥函式。函式簽名因鉤子名稱而異。

返回

此可組合項不返回值,但在元件的作用域被銷燬時會自動登出鉤子。

示例

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>