definePageMeta 是一个编译器宏,可用于为位于 app/pages/ 目录(除非另有配置)中的 页面 组件设置元数据。通过这种方式,你可以为 Nuxt 应用的每个静态或动态路由设置自定义元数据。
<script setup lang="ts">
definePageMeta({
layout: 'default',
})
</script>
export function definePageMeta (meta: PageMeta): void
interface PageMeta {
validate?: ((route: RouteLocationNormalized) => boolean | Promise<boolean> | Partial<NuxtError> | Promise<Partial<NuxtError>>)
redirect?: RouteRecordRedirectOption
name?: string
path?: string
props?: RouteRecordRaw['props']
alias?: string | string[]
pageTransition?: boolean | TransitionProps
layoutTransition?: boolean | TransitionProps
viewTransition?: boolean | 'always'
key?: false | string | ((route: RouteLocationNormalizedLoaded) => string)
keepalive?: boolean | KeepAliveProps
layout?: false | LayoutKey | Ref<LayoutKey> | ComputedRef<LayoutKey>
middleware?: MiddlewareKey | NavigationGuard | Array<MiddlewareKey | NavigationGuard>
scrollToTop?: boolean | ((to: RouteLocationNormalizedLoaded, from: RouteLocationNormalizedLoaded) => boolean)
[key: string]: unknown
}
metaPageMetanamestringapp/pages/ 目录 内的路径生成的。pathstringpropsRouteRecordRaw['props']params 作为 props 传入页面组件。aliasstring | string[]/users/:id 和 /u/:id。所有 alias 和 path 值必须共享相同的参数。keepaliveboolean | KeepAlivePropstrue,或者使用 KeepAliveProps 进行更细粒度的控制。keyfalse | string | ((route: RouteLocationNormalizedLoaded) => string)<NuxtPage> 组件何时重新渲染时,设置 key 值。layoutfalse | LayoutKey | Ref<LayoutKey> | ComputedRef<LayoutKey>false。layoutTransitionboolean | TransitionPropsfalse 以禁用布局过渡。middlewareMiddlewareKey | NavigationGuard | Array<MiddlewareKey | NavigationGuard>definePageMeta 中直接定义匿名或命名的中间件。了解更多关于路由中间件。pageTransitionboolean | TransitionPropsfalse 以禁用页面过渡。viewTransitionboolean | 'always'prefers-reduced-motion: reduce 时不应用过渡(推荐)。如果设置为 always,Nuxt 将始终应用过渡。redirectRouteRecordRedirectOptionvalidate(route: RouteLocationNormalized) => boolean | Promise<boolean> | Partial<NuxtError> | Promise<Partial<NuxtError>>statusCode/statusMessage 的对象以立即返回错误(不会检查其他匹配)。scrollToTopboolean | (to: RouteLocationNormalized, from: RouteLocationNormalized) => boolean~/router.options.ts 中进行设置(更多信息参见自定义路由)。[key: string]anymeta 对象的类型。下面的示例演示了:
key 如何可以是一个返回值的函数;keepalive 属性如何确保在多个组件之间切换时 <modal> 组件不会被缓存;pageType 作为自定义属性:<script setup lang="ts">
definePageMeta({
key: route => route.fullPath,
keepalive: {
exclude: ['modal'],
},
pageType: 'Checkout',
})
</script>
下面的示例展示了如何在 definePageMeta 中直接使用 function 定义中间件,或者设置为与位于 app/middleware/ 目录中的中间件文件名相匹配的 string:
<script setup lang="ts">
definePageMeta({
// define middleware as a function
middleware: [
function (to, from) {
const auth = useState('auth')
if (!auth.value.authenticated) {
return navigateTo('/login')
}
if (to.path !== '/checkout') {
return navigateTo('/checkout')
}
},
],
// ... or a string
middleware: 'auth',
// ... or multiple strings
middleware: ['auth', 'another-named-middleware'],
})
</script>
当路由重叠时,自定义正则表达式是解决冲突的一个好方法,例如:
两个路由 "/test-category" 和 "/1234-post" 都同时匹配 [postId]-[postSlug].vue 和 [categorySlug].vue 页面路由。
为确保在 [postId]-[postSlug] 路由中我们只匹配数字(\d+)作为 postId,可以在 [postId]-[postSlug].vue 页面模板中添加如下内容:
<script setup lang="ts">
definePageMeta({
path: '/:postId(\\d+)-:postSlug',
})
</script>
更多示例请参阅 Vue Router 的匹配语法。
你可以定义与(默认情况下)位于 app/layouts/ 目录 中布局文件名相匹配的布局。你也可以通过将 layout 设置为 false 来禁用布局:
<script setup lang="ts">
definePageMeta({
// set custom layout
layout: 'admin',
// ... or disable a default layout
layout: false,
})
</script>