生命周期钩子

Nuxt 提供了一个强大的钩子系统,可以扩展几乎所有的方面使用钩子。
Read more in Docs > Guide > Going Further > Hooks.

应用程序钩子(运行时)

查看 应用程序源代码 以获取所有可用的钩子。

钩子参数环境描述
app:createdvueAppServer Client在创建初始 vueApp 实例时调用。
app:errorerrServer Client当发生致命错误时调用。
app:error:cleared{ redirect? }Server Client当发生致命错误时调用。
app:data:refreshkeys?Server Client(内部)
vue:setup-Server Client(内部)
vue:errorerr, target, infoServer Client当 Vue 错误传播到根组件时调用。了解更多
app:renderedrenderContextServer当 SSR 渲染完成时调用。
app:redirected-Server在 SSR 重定向之前调用。
app:beforeMountvueAppClient在挂载应用程序之前调用,仅在客户端调用。
app:mountedvueAppClient当 Vue 应用程序在浏览器中初始化并挂载时调用。
app:suspense:resolveappComponentClientSuspense 解析事件。
app:manifest:update{ id, timestamp }Client检测到您的应用程序有更新版本时调用。
link:prefetchtoClient当观察到要预取的 <NuxtLink> 时调用。
page:startpageComponent?ClientSuspense 上调用挂起事件。
page:finishpageComponent?ClientSuspense 上调用解析事件。
page:loading:start-Client当新页面的 setup() 正在运行时调用。
page:loading:end-Clientpage:finish 后调用
page:transition:finishpageComponent?Client在页面转换后 onAfterLeave 事件。
dev:ssr-logslogsClient使用已传递给客户端的服务器端日志数组进行调用(如果启用了 features.devLogs)。
page:view-transition:starttransitionClient启用实验性视图转换支持后,调用 document.startViewTransition 时被调用。

Nuxt 钩子(构建时)

查看 schema 源代码 以查看所有可用的钩子。

钩子参数描述
kit:compatibilitycompatibility, issues允许扩展兼容性检查。
readynuxt在 Nuxt 初始化完成后调用,当 Nuxt 实例准备好工作时。
closenuxt当 Nuxt 实例优雅地关闭时调用。
restart{ hard?: boolean }被调用以重新启动当前的 Nuxt 实例。
modules:before-在 Nuxt 初始化期间调用,在安装用户模块之前。
modules:done-在 Nuxt 初始化期间调用,在安装用户模块之后。
app:resolveapp在解析 app 实例后调用。
app:templatesappNuxtApp 生成期间调用,允许自定义、修改或向构建目录添加新文件(可以是虚拟文件也可以写入 .nuxt)。
app:templatesGeneratedapp在模板被编译为虚拟文件系统(vfs)之后调用。
build:before-在 Nuxt 捆绑生成器之前调用。
build:done-在 Nuxt 捆绑生成器完成后调用。
build:manifestmanifest在 Vite 和 webpack 构建清单期间调用。这允许自定义 Nitro 将用于在最终 HTML 中呈现 <script><link> 标签的清单。
builder:generateAppoptionsCalled before generating the app.
builder:watchevent, pathCalled at build time in development when the watcher spots a change to a file or directory in the project.
pages:extendpagesCalled after page routes are scanned from the file system.
pages:resolvedpagesCalled after page routes have been augmented with scanned metadata.
pages:routerOptions{ files: Array<{ path: string, optional?: boolean }> }Called when resolving router.options files. Later items in the array override earlier ones.
server:devHandlerhandlerCalled when the dev middleware is being registered on the Nitro dev server.
imports:sourcespresetsCalled at setup allowing modules to extend sources.
imports:extendimportsCalled at setup allowing modules to extend imports.
imports:contextcontextCalled when the unimport context is created.
imports:dirsdirsAllows extending import directories.
components:dirsdirsCalled within app:resolve allowing to extend the directories that are scanned for auto-importable components.
components:extendcomponentsAllows extending new components.
nitro:confignitroConfigCalled before initializing Nitro, allowing customization of Nitro's configuration.
nitro:initnitroCalled after Nitro is initialized, which allows registering Nitro hooks and interacting directly with Nitro.
nitro:build:beforenitroCalled before building the Nitro instance.
nitro:build:public-assetsnitroCalled after copying public assets. Allows modifying public assets before Nitro server is built.
prerender:routesctxAllows extending the routes to be pre-rendered.
build:errorerrorCalled when an error occurs at build time.
prepare:typesoptionsCalled before Nuxi writes .nuxt/tsconfig.json and .nuxt/nuxt.d.ts, allowing addition of custom references and declarations in nuxt.d.ts, or directly modifying the options in tsconfig.json
listenlistenerServer, listenerCalled when the dev server is loading.
schema:extendschemasAllows extending default schemas.
schema:resolvedschemaAllows extending resolved schema.
schema:beforeWriteschemaCalled before writing the given schema.
schema:written-Called after the schema is written.
vite:extendviteBuildContextAllows to extend Vite default context.
vite:extendConfigviteInlineConfig, envAllows to extend Vite default config.
vite:configResolvedviteInlineConfig, envAllows to read the resolved Vite config.
vite:serverCreatedviteServer, envCalled when the Vite server is created.
vite:compiled-Called after Vite server is compiled.
webpack:configwebpackConfigsCalled before configuring the webpack compiler.
webpack:configResolvedwebpackConfigsAllows to read the resolved webpack config.
webpack:compileoptionsCalled right before compilation.
webpack:compiledoptionsCalled after resources are loaded.
webpack:changeshortPathCalled on change on WebpackBar.
webpack:error-Called on done if has errors on WebpackBar.
webpack:done-Called on allDone on WebpackBar.
webpack:progressstatesArrayCalled on progress on WebpackBar.

Nitro App 钩子(运行时、服务端)

查看 Nitro 获取所有可用的钩子。

HookArgumentsDescriptionTypes
dev:ssr-logs{ path, logs }ServerCalled at the end of a request cycle with an array of server-side logs.
render:responseresponse, { event }Called before sending the response.response, event
render:htmlhtml, { event }Called before constructing the HTML.html, event
render:islandislandResponse, { event, islandContext }Called before constructing the island HTML.islandResponse, event, islandContext
close-Called when Nitro is closed.-
errorerror, { event? }Called when an error occurs.error, event
requesteventCalled when a request is received.event
beforeResponseevent, { body }Called before sending the response.event, unknown
afterResponseevent, { body }Called after sending the response.event, unknown