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

nuxt.renderRoute(route, context)

使用传入的上下文渲染特定路由。


  • 类型: Function
  • 参数:
    1. String:要渲染的路由
    2. 可选 Object,传入的上下文,可用键:reqres
  • 返回值: Promise
    • html: String
    • error: nullObject
    • redirected: falseObject

此方法大多数情况下与 nuxt.renderAndGetWindow 一起用于测试目的。

nuxt.renderRoute 最好在生产模式下构建完成后执行。
const { loadNuxt, build } = require('nuxt')

async function start() {
  // 获取用于启动的 nuxt 实例(生产模式)
  // 运行此脚本前请确保已执行 `nuxt build`
  const nuxt = await loadNuxt({ for: 'start' })

  const { html, error, redirected } = await nuxt.renderRoute('/')

  // `html` 始终为字符串

  // 显示错误布局时 `error` 不为 null,错误格式如下:
  // { statusCode: 500, message: 'My error message' }

  // 在 `asyncData()` 或 `fetch()` 中使用了 `redirect()` 时 `redirected` 不为 `false`
  // { path: '/other-path', query: {}, status: 302 }
}

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