nuxt.renderRoute(route, context)
使用传入的上下文渲染特定路由。
-
类型:
Function -
参数:
-
String:要渲染的路由 -
可选
Object,传入的上下文,可用键:req和res
-
-
返回值:
Promise-
html:String -
error:null或Object -
redirected:false或Object
-
此方法大多数情况下与 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