Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
54 captures
16 Nov 2022 - 08 Oct 2025
JulAUGSep
16
202220232024
success
fail
COLLECTED BY
Collection:Save Page Now
TIMESTAMPS
loading
The Wayback Machine - http://web.archive.org/web/20230816133112/https://nuxt.com/docs/guide/directory-structure/layouts
🎓 Official Vue.js Certification - The official certification of competence for the Vue.js Framework is out.Discover

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.

Layouts are placed in thelayouts/ directory and will be automatically loaded via asynchronous import when used. Layouts are used by adding<NuxtLayout> to yourapp.vue, and either setting alayout property as part of your page metadata (if you are using the~/pages integration), or by manually specifying it as a prop to<NuxtLayout>. (Note: The layout name is normalized to kebab-case, sosomeLayout becomessome-layout.)

If you only have a single layout in your application, we recommend usingapp.vue instead.

Unlike other components, your layouts must have a single root element to allow Nuxt to apply transitions between layout changes - and this root element cannot be a<slot />.

Enabling the Default Layout

Add a~/layouts/default.vue:

layouts/default.vue
<template>  <div>    Some default layout shared across all pages    <slot/>  </div></template>

In a layout file, the content of the layout will be loaded in the<slot />, rather than using a special component.

If you use aapp.vue you will also need to add<NuxtLayout>:

app.vue
<template>  <NuxtLayout>    some page content  </NuxtLayout></template>

Setting Another Layout

-|layouts/---|default.vue---|custom.vue

You can directly override the default layout like this:

app.vue
<scriptsetuplang="ts">// You might choose this based on an API call or logged-in statusconstlayout="custom";</script><template>  <NuxtLayout:name="layout">    <NuxtPage />  </NuxtLayout></template>

Alternatively, you can override the default layout per-page like this:

pages/index.vue
<script>// This will work in both `<script setup>` and `<script>`definePageMeta({layout:"custom",});</script>
app.vue
<template>  <NuxtLayout>    <NuxtPage />  </NuxtLayout></template>
layouts/custom.vue
<template>  <div>    Some *custom* layout    <slot/>  </div></template>
layouts/default.vue
<template>  <div>    A *default* layout    <slot/>  </div></template>

Learn more aboutdefining page meta.

Changing the Layout Dynamically

You can also use a ref or computed property for your layout.

<scriptsetuplang="ts">functionenableCustomLayout () {setPageLayout('custom')}definePageMeta({layout:false,});</script><template>  <div>    <button@click="enableCustomLayout">Update layout</button>  </div></template>
Read and edit a live example inDocs > Examples > Features > Layouts.

Overriding a Layout on a Per-page Basis

If you are using the~/pages integration, you can take full control by settinglayout: false and then using the<NuxtLayout> component within the page.

pages/index.vue
<scriptsetuplang="ts">definePageMeta({layout:false,});</script><template>  <div>    <NuxtLayoutname="custom">      <template #header> Some header template content. </template>      The rest of the page    </NuxtLayout>  </div></template>
layouts/custom.vue
<template>  <div>    <header>      <slotname="header">        Default header content      </slot>    </header>    <main>      <slot/>    </main>  </div></template>

If you use<NuxtLayout> within your pages, make sure it is not the root element (or disable layout/page transitions).


[8]ページ先頭

©2009-2025 Movatter.jp