In this episode, we dive into how to use Nuxt's layouts feature to better structure the different sections of your site. Up until now, we've just been throwing everything – including our navigation – into app.vue
, but that's not really scalable. If you want different layouts for, say, your homepage and your account area, it gets messy fast.
So, we start by creating a layouts
directory in the root of our project. Here, we move our navigation markup into a new default.vue
layout. This layout wraps all our pages automatically, keeping the navigation consistent without bloating app.vue
.
Then, we look at how to set up custom layouts. For example, if our account area needs a special navigation bar, we create an account.vue
layout. We show how you can tell a specific page (like account/index.vue
) to use this new layout instead of the default one by exporting a layout property from its script section. That way, your account pages look different from the rest of your site, all with minimal fuss.
You'll learn how layouts keep your code much tidier and make it easy to have different sections of your site share or vary key layout elements. We also see that using script setup
still works nicely alongside specifying a custom layout. By the end, you've got a default and a custom layout in play, and you know exactly when and how to use them!