defineRouteRules

来源
在页面级别为混合渲染定义路由规则。
此功能为实验性特性,使用前需在 nuxt.config 中启用 experimental.inlineRouteRules 选项。

Usage

app/pages/index.vue
<script setup lang="ts">
defineRouteRules({
  prerender: true,
})
</script>

<template>
  <h1>Hello world!</h1>
</template>

将被转换为:

nuxt.config.ts
export default defineNuxtConfig({
  routeRules: {
    '/': { prerender: true },
  },
})
在运行 nuxt build 时,主页将被预渲染到 .output/public/index.html 并以静态方式提供。

Notes

  • ~/pages/foo/bar.vue 中定义的规则会应用于 /foo/bar 请求。
  • ~/pages/foo/[id].vue 中的规则会应用于 /foo/** 请求。

若需更细粒度的控制(例如在页面的 definePageMeta 中使用了自定义的 pathalias),应在 nuxt.config 中直接设置 routeRules

阅读有关 routeRules 的更多信息。