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


nuxt.renderAndGetWindow(url, options)

从 Nuxt 应用程序的给定 URL 获取 window 对象。


  • 类型: Function
  • 参数: String
    1. String:要渲染的 URL
    2. 可选 Object:选项
    • virtualConsole: Boolean(默认: true
  • 返回值: Promise
    • 返回值: window
此方法仅用于测试目的。

使用此函数需要安装 jsdom

npm install --save-dev jsdom

示例:

const { loadNuxt } = require('nuxt')

async function init() {
  // 假设已构建好项目
  const nuxt = await loadNuxt({ for: 'start' })
  await nuxt.listen(3000)
  const window = await nuxt.renderAndGetWindow('http://localhost:3000')
  // 打印 head `<title>`
  console.log(window.document.title)
  nuxt.close()
}

init()

接下来?

Edit this page on GitHub Updated at Tue, Apr 14, 2026