Nuxt 3 Crash Course #6 - Layouts

Net Ninja · Beginner ·🌐 Frontend Engineering ·3y ago

Key Takeaways

This video tutorial covers creating layout files for pages in Nuxt 3, including default and custom layouts, and applying them to specific pages using the DefinePageMeta composable function.

Full Transcript

all right then my friends so in the last lesson we used these next link components to create links in the page and we put those inside a header and that header is currently on the about page and also the index page now ideally I'd want to place this on other Pages as well if we created them in the future and maybe the products page so what I'd have to do is copy this and paste it inside each of those components and then whenever there's a change to the header I'd have to go through each of those components and update the header right now that's probably not the best way of doing it because we're repeating the code and taking loads of time to update the code if we need to so instead in next we can create layouts where we just defined the code once that's common to every page and then if we want to update it we only have to do it once and that layout is going to wrap all of these different page components in so how do we do this how do we create layouts that's simple in the root directory create a folder and that's going to be called layouts and then we can create different kinds of layouts but if you want a default layout that's going to basically wrap every single page in your application you can create that by making a file called default Dot View like so and all I want to do inside this is copy all of this template right here so let me grab that and paste it in I want to get rid of the home page content because we don't need that we just basically want this header right here and then after the header this right here is where we want to Output page content right so how do we do that well we use a component called slot so I'm going to do a div tag first of all and then inside that I'm going to use a component called slot like so and this is a built-in component that next gives us and when it sees this inside the layout it says okay well this is where the page content is going to go so that's it now automagically next is going to look for this default layout and it's going to wrap all of our Different Page components and those page components are going to be output right here where the slot component is all right so what I'm also going to do is just add a style to this down here I'm going to paste it in and all we're doing is styling those active links remember in the last lesson I showed you when we had these nuxed link components when we're on that current page it applies classes to them to say that they're active so we're styling one of those classes so it colors that link a little bit differently so I'm going to save this now and then we can go back to the index component and we can get rid of the header from here because now it's inside the layout file save that and also go to the about component and get rid of it from here as well so let's save this and preview now in a browser oh right then so in a browser we can see we still get that nav bar at the top awesome as we should do everything's working the same way and we can go between these different components and also if we go to the products page it should be showing on there because that layout component is now being applied to all pages in the site next takes care of that for us now we don't need this thing right here so I'll delete that in a second but also I want to show you how we can create custom layouts as well that we can apply to certain pages for example we might want the default layout for all of these Pages like home and about or contact if we had one but then we might want a different type of layout for the products page and also the products ID page and by the way if we go to that so one two three we should get that layout as well so how do we create these custom layouts well it's pretty simple to do all we have to do is create a new file inside the layouts folder so let's do that and you can call this what you want I'm calling it products.view because I want this to apply to all of the different product pages now it doesn't have to be called products if you want it to apply to the products Pages these names don't have to match the folder name and this layout name it can be called ABC and I could still apply it to anything inside this products folder it doesn't matter but I'm just naming it products because it's obvious to me now that this is going to be the layout file for all of the different product pages okay then so inside here I'm basically going to go to the other layout copy that and paste it in and I'm just going to change this a little bit so up here I want to first of all get rid of that anchor tag and also go to the default layout and get rid of that as well we don't need that so let's save it and then I'm going to grab this UL right here and cut it and then I'm going to paste that maybe at the bottom of the page so let's do a footer down at the bottom then inside here I'm going to Output these notched link components so it's pretty simple we're basically just splitting the template into two different parts we have the header at the top which has the title and in fact we'll say next Dojo merch because we're now on the products page and if you click on this it's just going to take us now to forward slash products and not back to the home page and then down here we have all of the old site links as well so just a slightly different template so now we have this product template right here this product layout but how do we then apply it to the different pages how do we say for example that look we want this index component inside the products folder to have that products layout well what we can do is use a special composable function that next gives us inside the script called Define page meta and this function accepts as an argument an object and there we can specify a layout property to specify which layout that we want to use now it's telling us we have default our products so we just want to use the products layout and now the products layout will be applied to this particular page so I'm going to save that I'm also going to copy this and go to the ID page over here and paste it here as well so that for both of these Pages we're using the products layout all right so now on the home page we can see the default layout on the about page we can see the default layout if we go to product it's going to change slightly the links are going to go to the bottom and we can see that we get the title up here next Dojo merge which goes to forward slash products and also beneath the actual content we get these links so if I go to forward slash 123 it should be the same layout as this page press enter and we can see again we get the alternate layout where the title is at the top then the content then The Links at the bottom awesome so that's how we use layouts in next

Original Description

In this Nuxt lesson, you'll lear how to make layout files for your pages. ⭐⭐ Watch the whole course now (without ads) on Net Ninja Pro: https://netninja.dev/p/nuxt-3-crash-course 🐱‍💻 Access the course files on GitHub: Course files - https://github.com/iamshaunjp/nuxt-3-tutorial 🐱‍💻 Vue 3 Tutorial: On Net Ninja Pro - https://netninja.dev/p/build-websites-with-vue-3-firebase On YouTube - https://www.youtube.com/watch?v=YrxBCBibVo0&list=PL4cUxeGkcC9hYYGbV60Vq3IXYNfDk8At1 🐱‍💻 Tailwind Tutorial: On Net Ninja Pro - https://netninja.dev/p/tailwind-css-tutorial On YouTube - https://www.youtube.com/watch?v=bxmDnn7lrnk&list=PL4cUxeGkcC9gpXORlEHjc5bgnIi5HEGhw 🐱‍💻 Nuxt Docs - https://code.visualstudio.com/ 🐱‍💻 VS Code - https://v3.nuxtjs.org/getting-started/installation
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This tutorial teaches how to create and apply layouts in Nuxt 3, including default and custom layouts, to manage page templating and improve frontend performance.

Key Takeaways
  1. Create a layouts folder in the root directory
  2. Create a default layout file (e.g., default.vue)
  3. Use the Slot component to output page content
  4. Create custom layout files (e.g., products.vue)
  5. Apply custom layouts to specific pages using DefinePageMeta
  6. Use the DefinePageMeta composable function to specify layout properties
💡 Using layouts in Nuxt 3 allows for efficient management of page templating and improves frontend performance by reducing code duplication and improving page loading times.

Related Reads

📰
Tags, Releases, and Branches: A Practical Guide to Frontend Deployment
Learn how to streamline frontend deployment with a disciplined branching and release process
Medium · Programming
📰
Tags, Releases, and Branches: A Practical Guide to Frontend Deployment
Learn how to implement a disciplined branching and release process for frontend deployment to improve production visibility
Medium · DevOps
📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Learn how to prepare for a Frontend SDE-2 interview at Wayfair, including online assessments, machine coding, and system design.
Medium · Programming
📰
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Learn how HTMX rebuilt a React SPA in a week, replacing 2 years of maintenance work, and discover the benefits of this alternative approach
Medium · Programming
Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →