2022-10-06 09:15:30 +00:00
---
title: "< NuxtLayout > "
2023-10-18 10:59:43 +00:00
description: "Nuxt provides the < NuxtLayout > component to show layouts on pages and error pages."
links:
- label: Source
icon: i-simple-icons-github
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/components/nuxt-layout.ts
size: xs
2022-10-06 09:15:30 +00:00
---
2023-10-18 10:59:43 +00:00
You can use `<NuxtLayout />` component to activate the `default` layout on `app.vue` or `error.vue` .
2022-04-06 05:56:08 +00:00
2023-10-18 10:59:43 +00:00
```vue [app.vue]
2022-08-02 13:43:40 +00:00
< template >
< NuxtLayout >
some page content
< / NuxtLayout >
< / template >
```
2023-10-18 10:59:43 +00:00
:read-more{to="/docs/guide/directory-structure/layouts"}
2022-08-02 13:43:40 +00:00
2023-10-18 10:59:43 +00:00
## Props
2022-08-02 13:43:40 +00:00
2023-10-18 10:59:43 +00:00
- `name` : Specify a layout name to be rendered, can be a string, reactive reference or a computed property. It **must** match the name of the corresponding layout file in the [`layouts/` ](/docs/guide/directory-structure/layouts ) directory.
- **type**: `string`
- **default**: `default`
2022-08-02 13:43:40 +00:00
```vue [pages/index.vue]
2023-07-18 10:31:45 +00:00
< script setup lang = "ts" >
// layouts/custom.vue
const layout = 'custom'
< / script >
2022-08-02 13:43:40 +00:00
< template >
< NuxtLayout :name = "layout" >
< NuxtPage / >
< / NuxtLayout >
< / template >
```
2024-02-21 17:09:45 +00:00
::note
2022-08-02 13:43:40 +00:00
Please note the layout name is normalized to kebab-case, so if your layout file is named `errorLayout.vue` , it will become `error-layout` when passed as a `name` property to `<NuxtLayout />` .
2022-04-06 05:56:08 +00:00
::
2023-10-18 10:59:43 +00:00
```vue [error.vue]
2022-08-02 13:43:40 +00:00
< template >
< NuxtLayout name = "error-layout" >
< NuxtPage / >
< / NuxtLayout >
< / template >
```
2023-10-18 10:59:43 +00:00
::read-more{to="/docs/guide/directory-structure/layouts"}
Read more about dynamic layouts.
::
2024-03-05 20:16:06 +00:00
- `fallback` : If an invalid layout is passed to the `name` prop, no layout will be rendered. Specify a `fallback` layout to be rendered in this scenario. It **must** match the name of the corresponding layout file in the [`layouts/` ](/docs/guide/directory-structure/layouts ) directory.
- **type**: `string`
- **default**: `null`
2023-10-18 10:59:43 +00:00
## Additional Props
2023-09-05 10:09:23 +00:00
`NuxtLayout` also accepts any additional props that you may need to pass to the layout. These custom props are then made accessible as attributes.
2023-10-18 10:59:43 +00:00
```vue [pages/some-page.vue]
< template >
< div >
< NuxtLayout name = "custom" title = "I am a custom layout" >
< -- . . . -- >
< / NuxtLayout >
< / div >
< / template >
2023-09-05 10:09:23 +00:00
```
In the above example, the value of `title` will be available using `$attrs.title` in the template or `useAttrs().title` in `<script setup>` at custom.vue.
2023-10-18 10:59:43 +00:00
```vue [layouts/custom.vue]
2023-09-05 10:09:23 +00:00
< script setup lang = "ts" >
2023-10-18 10:59:43 +00:00
const layoutCustomProps = useAttrs()
console.log(layoutCustomProps.title) // I am a custom layout
2023-09-05 10:09:23 +00:00
< / script >
```
2023-10-18 10:59:43 +00:00
## Transitions
2022-08-02 13:43:40 +00:00
`<NuxtLayout />` renders incoming content via `<slot />` , which is then wrapped around Vue’ s `<Transition />` component to activate layout transition. For this to work as expected, it is recommended that `<NuxtLayout />` is **not** the root element of the page component.
2024-03-28 18:06:16 +00:00
::code-group
2022-08-02 13:43:40 +00:00
```vue [pages/index.vue]
< template >
< div >
< NuxtLayout name = "custom" >
< template #header > Some header template content. </ template >
< / NuxtLayout >
< / div >
< / template >
```
2024-03-28 18:06:16 +00:00
```vue [layouts/custom.vue]
< template >
< div >
<!-- named slot -->
< slot name = "header" / >
< slot / >
< / div >
< / template >
```
::
2023-10-18 10:59:43 +00:00
:read-more{to="/docs/getting-started/transitions"}
## Layout's Ref
2023-06-10 22:17:14 +00:00
2023-11-18 20:24:13 +00:00
To get the ref of a layout component, access it through `ref.value.layoutRef` .
2023-06-10 22:17:14 +00:00
2024-03-28 18:06:16 +00:00
::code-group
```vue [app.vue]
2023-06-10 22:17:14 +00:00
< script setup lang = "ts" >
const layout = ref()
2023-10-18 10:59:43 +00:00
2023-06-10 22:17:14 +00:00
function logFoo () {
layout.value.layoutRef.foo()
}
< / script >
2023-07-18 10:31:45 +00:00
< template >
2024-03-28 18:06:16 +00:00
< NuxtLayout ref = "layout" >
default layout
< / NuxtLayout >
2023-07-18 10:31:45 +00:00
< / template >
2024-03-28 18:06:16 +00:00
```
```vue [layouts/default.vue]
< script setup lang = "ts" >
const foo = () => console.log('foo')
defineExpose({
foo
})
< / script >
< template >
< div >
default layout
< slot / >
< / div >
< / template >
```
::
2023-06-10 22:17:14 +00:00
2023-10-18 10:59:43 +00:00
:read-more{to="/docs/guide/directory-structure/layouts"}