setPageLayout
setPageLayout 允许你动态更改页面的布局。
Maschinelle Übersetzung ·0 % geprüft
setPageLayout 允许你动态更改页面的布局。它依赖于对 Nuxt context 的访问,因此只能在 Nuxt context 中调用。app/middleware/custom-layout.ts
export default defineNuxtRouteMiddleware((to) => {
// 在你要导航到的路由上设置布局
setPageLayout('other')
})向布局传递 Props v4.3
你可以通过提供一个对象作为第二个参数,将 props 传递给布局:
app/middleware/admin-layout.ts
export default defineNuxtRouteMiddleware((to) => {
setPageLayout('admin', {
sidebar: true,
title: 'Dashboard',
})
})然后布局就可以接收这些 props:
app/layouts/admin.vue
<script setup lang="ts">
const props = defineProps<{
sidebar?: boolean
title?: string
}>()
</script>
<template>
<div>
<aside v-if="sidebar">
Sidebar
</aside>
<main>
<h1>{{ title }}</h1>
<slot />
</main>
</div>
</template>如果你选择在服务器端动态设置布局,你必须在 Vue 渲染布局之前(即在插件或路由中间件中)执行此操作,以避免水合不匹配。