8000 docs: add example of passing props to layouts · nuxt/nuxt@401aa90 · GitHub
[go: up one dir, main page]

Skip to content

Commit 401aa90

Browse files
committed
docs: add example of passing props to layouts
1 parent 4407f68 commit 401aa90

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/4.api/3.utils/set-page-layout.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,40 @@ export default defineNuxtRouteMiddleware((to) => {
1919
})
2020
```
2121

22+
## Passing Props to Layouts
23+
24+
You can pass props to the layout by providing an object as the second argument:
25+
26+
```ts [app/middleware/admin-layout.ts]
27+
export default defineNuxtRouteMiddleware((to) => {
28+
setPageLayout('admin', {
29+
sidebar: true,
30+
title: 'Dashboard',
31+
})
32+
})
33+
```
34+
35+
The layout can then receive these props:
36+
37+
```vue [app/layouts/admin.vue]
38+
<script setup lang="ts">
39+
const props = defineProps<{
40+
sidebar?: boolean
41+
title?: string
42+
}>()
43+
</script>
44+
45+
<template>
46+
<div>
47+
<aside v-if="sidebar">Sidebar</aside>
48+
<main>
49+
<h1>{{ title }}</h1>
50+
<slot />
51+
</main>
52+
</div>
53+
</template>
54+
```
55+
2256
::note
2357
If you choose to set the layout dynamically on the server side, you _must_ do so before the layout is rendered by Vue (that is, within a plugin or route middleware) to avoid a hydration mismatch.
2458
::

0 commit comments

Comments
 (0)
0