8000 docs: add information on how to type custom hooks by huang-julien Β· Pull Request #22312 Β· nuxt/nuxt Β· GitHub
[go: up one dir, main page]

Skip to content

docs: add information on how to type custom hooks #22312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions docs/2.guide/3.going-further/2.hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ App hooks can be mainly used by [Nuxt Plugins](/docs/guide/directory-structure/p
```js [plugins/test.ts]
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('page:start', () => {
/* your code goes here */
})
/* your code goes here */
})
})
```

Expand Down Expand Up @@ -73,3 +73,26 @@ export default defineNitroPlugin((nitroApp) => {
::alert{icon=πŸ‘‰}
Learn more about available [Nitro lifecycle hooks](/docs/api/advanced/hooks#nitro-app-hooks-runtime-server-side).
::

## Add additional hooks

You can add additional hooks by augmenting the types provided by Nuxt. This can be useful for modules.

```ts
import { HookResult } from "@nuxt/schema";

declare module '#app' {
interface RuntimeNuxtHooks {
'your-nuxt-runtime-hook': () => HookResult
}
interface NuxtHooks {
'your-nuxt-hook': () => HookResult
}
}

declare module 'nitropack' {
interface NitroRuntimeHooks {
'your-nitro-hook': () => void;
}
}
```
0