Nuxt/docs/content/3.docs/2.directory-structure/6.layouts.md
2021-10-21 12:14:40 +02:00

2.4 KiB

icon title head.title
IconDirectory layouts Layouts directory

Layouts directory

Nuxt provides a customizable layouts framework you can use throughout your application, ideal for extracting common UI or code patterns into reusable layout components.

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 options.

If you have only single layout for application, you can alternatively use app.vue.

Example: a custom layout

-| 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:

<template>
  <div>
    Some shared layout content:
    <slot />
  </div>
</template>

Given the example above, you can use a custom layout like this:

<script>
export default {
  layout: "custom",
};
</script>

Example: using with slots

You can also take full control (for example, with slots) by using the <NuxtLayout> component (which is globally available throughout your application) and set layout: false in your component options.

<template>
  <NuxtLayout name="custom">
    <template #header> Some header template content. </template>

    The rest of the page
  </NuxtLayout>
</template>

<script>
export default {
  layout: false,
};
</script>

Example: using with <script setup>

If you are utilizing Vue <script setup> compile-time syntactic sugar, you can use a secondary <script> tag to set layout options as needed.

::alert{type=info} Learn more about <script setup> and <script> tags co-existing in the Vue docs. ::

Assuming this directory structure:

-| layouts/
---| custom.vue
-| pages/
---| my-page.vue

And this custom.vue layout:

<template>
  <div>
    Some shared layout content:
    <slot />
  </div>
</template>

You can set a page layout in my-page.vue — alongside the <script setup> tag — like this:

<script>
export default {
  layout: "custom",
};
</script>

<script setup>
// your setup script
</script>