Layouts in Nuxt 3 and Nuxt 2 have some minor changes. In Nuxt 2 we have used <Nuxt/> component to render pages / routes (if you using ~pages ) .
Nuxt 3 replaced the Nuxt component with <Slot/>.
How to create a Layout ?
To create and apply custom layout, we need to create Layouts folder and create our layouts.
//custom.vue
<template>
<div data-theme="winter" class="">
<header>
<Header/>
</header>
<main class="h-screen w-full grid overflow-auto ">
<slot/>
</main>
<footer>
<Footer/>
</footer>
</div>
</template>
Applying Layout
We can apply layout to pages using the the defineMeta section in the page component.
//pages/index.vue
<template>
<div class="flex justify-center">
<div class="p-1 h-full w-3/4 ">
<Hero />
</div>
</div>
</template>
<script setup>
definePageMeta({
layout: "custom",
});
</script>
It also require to wrap the root component with <NuxtLayout> as follows
//app.vue
<template>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
Applying layout in a template
We can apply templates using the NuxtLayout component directly into the HTML. Wrap the components with the NuxtLayout and pass the layout name.
<template>
<NuxtLayout name='custom'>
<h1>Hello world</h1>
</NuxtLayout>
</template>
Default layout
To create a default layout , create a component file with Layouts/default.vue and will be used for all pages with no Layout specified.
Disabling Layout
Suppose you want disable default layout for particular page, pass layout as false.
definePageMeta({
layout:false,
});
- Create a contentful plugin nuxt 3
- How to build a blog using Nuxt 3 and Graphql
- Custom 404 route in Nuxt 3
- Access dynamic route params in Nuxt 3
- Setup Graphql client in Nuxt3
- How to enable custom layout for routes in Nuxt3
- How to integrate Strapi in Nuxt3
- Nuxt 3 Pinia store example
- How to create Vue 3 el check box filter
- Use props in script setup-Nuxt3