mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-13 17:43:59 +00:00
46 lines
1013 B
Markdown
46 lines
1013 B
Markdown
|
# Lifecycle Hooks
|
||
|
|
||
|
Nuxt provides a powerful hooking system to expand almost every aspect using hooks powered by [unjs/hookable](https://github.com/unjs/hookable).
|
||
|
|
||
|
## Nuxt Hooks (build time)
|
||
|
|
||
|
This hooks are available for [Nuxt Modules](/guide/going-further/modules) and build context.
|
||
|
|
||
|
### Usage with `nuxt.config`
|
||
|
|
||
|
```js [nuxt.config]
|
||
|
export default defineNuxtConfig({
|
||
|
hooks: {
|
||
|
'close': () => { }
|
||
|
}
|
||
|
})
|
||
|
```
|
||
|
|
||
|
### Usage with Nuxt Modules
|
||
|
|
||
|
```js
|
||
|
import { defineNuxtModule } from '@nuxt/kit'
|
||
|
|
||
|
export default defineNuxtModule({
|
||
|
setup (options, nuxt) {
|
||
|
nuxt.hook('close', async () => { })
|
||
|
})
|
||
|
})
|
||
|
```
|
||
|
|
||
|
## App Hooks (runtime)
|
||
|
|
||
|
App hooks can be mainly used by [Nuxt Plugins](/guide/directory-structure/plugins) to hook into rendering lifecycle but could also be used in Vue composables.
|
||
|
|
||
|
### Usage with Plugins
|
||
|
|
||
|
```js [plugins/test.ts]
|
||
|
export defineNuxtPlugin(nuxtApp) {
|
||
|
nuxtApp.hook('page:start', () => { })
|
||
|
}
|
||
|
```
|
||
|
|
||
|
::alert{icon=👉}
|
||
|
Learn more about [available lifecycle hooks](/api/advanced/hooks)
|
||
|
::
|