setPageLayout

来源
setPageLayout 允许你动态更改页面的布局。
setPageLayout 允许你动态更改页面的布局。它依赖于访问 Nuxt 上下文,因此只能在Nuxt 上下文内调用。
app/middleware/custom-layout.ts
export default defineNuxtRouteMiddleware((to) => {
  // 在你要导航到的路由上设置布局
  setPageLayout('other')
})

Passing Props to Layouts

You can pass props to the layout by providing an object as the second argument:

app/middleware/admin-layout.ts
export default defineNuxtRouteMiddleware((to) => {
  setPageLayout('admin', {
    sidebar: true,
    title: 'Dashboard',
  })
})

The layout can then receive these 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 渲染布局之前(即在插件或路由中间件中)执行此操作,以避免水合(hydration)不匹配。