Nuxt/docs/content/3.docs/2.directory-structure/6.layouts.md

89 lines
2.0 KiB
Markdown
Raw Normal View History

---
icon: IconDirectory
title: 'layouts'
head.title: Layouts directory
---
# Layouts directory
2021-06-30 16:32:22 +00:00
Nuxt provides a customizable layouts framework you can use throughout your application, ideal for extracting common UI or code patterns into reusable layout components.
2021-11-21 12:31:44 +00:00
Page layouts are placed in the `layouts/` directory and will be automatically loaded via asynchronous import when used. If you create a `layouts/default.vue` this will be used for all pages in your app. Other layouts are used by setting a `layout` property as part of your component's options.
2021-06-30 16:32:22 +00:00
2021-11-21 12:31:44 +00:00
If you only have a single layout in your application, you can alternatively use [app.vue](/docs/directory-structure/app).
2021-06-30 16:32:22 +00:00
## Example: a custom layout
2021-06-30 16:32:22 +00:00
```bash
-| layouts/
---| custom.vue
```
In your layout files, you'll need to use `<slot />` to define where the page content of your layout will be loaded. For example:
```vue
<template>
<div>
Some shared layout content:
<slot />
</div>
</template>
```
Given the example above, you can use a custom layout like this:
```vue
<script>
// This will also work in `<script setup>`
definePageMeta({
2021-06-30 16:32:22 +00:00
layout: "custom",
});
2021-06-30 16:32:22 +00:00
</script>
```
::alert{type=info}
Learn more about [defining page meta](/docs/directory-structure/pages#page-metadata).
::
## Example: using with slots
2021-06-30 16:32:22 +00:00
You can also take full control (for example, with slots) by using the `<NuxtLayout>` component (which is globally available throughout your application) by setting `layout: false`.
2021-06-30 16:32:22 +00:00
```vue
<template>
<NuxtLayout name="custom">
<template #header> Some header template content. </template>
The rest of the page
</NuxtLayout>
</template>
<script setup>
definePageMeta({
2021-06-30 16:32:22 +00:00
layout: false,
});
2021-06-30 16:32:22 +00:00
</script>
```
## Example: changing the layout
You can also use a ref or computed property for your layout.
```vue
<template>
<div>
<button @click="enableCustomLayout">Update layout</button>
</div>
</template>
<script setup>
const route = useRoute()
function enableCustomLayout () {
route.meta.layout = "custom"
}
definePageMeta({
layout: false,
});
</script>
```