您正在浏览 Nuxt 2 文档。前往 Nuxt 3 文档, 或了解更多关于 Nuxt 2 长期支持

ModuleContainer 类


所有模块 都在 ModuleContainer 实例的上下文中被调用。

Tapable 插件

可以在特定生命周期事件上注册钩子。

nuxt.moduleContainer.plugin('ready', async moduleContainer => {
  // 所有模块准备就绪后执行此操作
})

模块 上下文中,可以改用以下方式:

this.plugin('ready', async moduleContainer => {
  // 所有模块准备就绪后执行此操作
})
插件 参数 时机
ready moduleContainer nuxt.config.js 中所有模块初始化完成时

方法

addVendor (vendor)

vendor 已废弃,不再使用

options.build.vendor 添加并应用唯一过滤器。

addTemplate (template)

  • template: StringObject
    • src
    • options
    • fileName

在构建期间,使用 lodash template 将给定模板渲染到项目的 buildDir.nuxt)中。

如果未提供 fileNametemplate 为字符串,目标文件名默认为 [dirName].[fileName].[pathHash].[ext]

此方法返回最终的 { dst, src, options } 对象。

addPlugin (template)

  • template: 对象属性(srcoptionsfileNamemode)。

使用 addTemplate 注册插件并将其添加到 plugins[] 数组的开头。

this.addPlugin({
  src: path.resolve(__dirname, 'templates/foo.js'),
  fileName: 'foo.server.js' // [可选] 仅包含在服务端 bundle 中
  options: moduleOptions
})

注意: 如果只在客户端或服务端使用插件,可以使用 mode 或在 fileName 选项中使用 .client.server 修饰符(可用选项请参阅 plugins )。

如果指定了 fileName,可以为 fileName 设置自定义路径,可以选择 .nuxt 文件夹内的文件夹结构以防止名称冲突:

{
  fileName: path.join('folder', 'foo.client.js'), // 结果为 `.nuxt/folder/foo.client.js`
}

addServerMiddleware (middleware)

将中间件推送到 options.serverMiddleware

extendBuild (fn)

通过链接 options.build.extend 函数,轻松扩展 webpack 构建配置。

extendRoutes (fn)

通过链接 options.build.extendRoutes 函数,轻松扩展路由。

addModule (moduleOpts, requireOnce)

异步函数

注册模块。moduleOpts 可以是字符串或数组([src, options])。如果 requireOncetrue 且解析的模块导出了 meta,则可避免同一模块被注册两次。

requireModule (moduleOpts)

异步函数

addModule(moduleOpts, true) 的简写。

钩子

可以在特定生命周期事件上注册钩子。

钩子 参数 时机
modules:before (moduleContainer, options) 创建 ModuleContainer 类之前调用,可用于重载方法和选项。
modules:done (moduleContainer) 所有模块加载完成时调用。
Edit this page on GitHub Updated at Tue, Apr 14, 2026