Svelte Tutorial for Beginners #14 - Slots
Key Takeaways
Explains the use of slots in Svelte for dynamic content
Full Transcript
alright then gang so we have our model now which opens and closes correctly as it should do and we also pass in this message as a prop to render inside the model now that's fine but what if we wanted to render something a bit more complex inside this model passing something like an HTML form that we can show well this method of using a prop is not really going to cut the mustard because we can't just send in a load of HTML as a prop so instead what we've use is something called slots in belts and slots give us a way to pass child content into a component and then render that content inside the component so how does this work well first of all let's get rid of the prop because we're not going to use that anymore and let's get rid of the message here and also the message right here so the way this works is that instead of this being a self closing tag we have an opening tag and a closing tag and then any Content that we pass down here as a child to the modal can be accessed as a slot inside this component itself so for example let me do an h3 right here and I'll say add a new person so below here we'll do a form and this form is going to be for adding a new person to the people array that we have up here so inside this form all I'm going to do at the minute is a couple of imports of tag text we'll give each one a placeholder as well so this first one is going to be for the name and then I'm going to duplicate this and the second one is going to be for the belt color and then below that we'll also do a button which says add person like so now we've not hooked up this functionality yet I'm just showing you how to use slots at the minute and we'll look all the functionality later on when we talk about forms but for now how do we then take this stuff that we pass through to the modal component right here as a child how do we take that and render it over here well we do it by using the slot keyword or the slot attack so inside the modal this is where we want to output it right all we need to do is say slot like so and we close it with a closing slot tag and then that is going to look for any child contents inside the modal and it's going to output it or right here where the slot is okay so if we save that and preview all we should see that form inside the modal which we do awesome so that's a simple way to take content and pass it in as a slot to another component right now we can also use something called named slots and the way we do that is by for example creating some more content right here and let me just take this and paste it in right here and in fact I'm going to comment this out at the top and I'm gonna give this a slot property and I'm gonna set that equal to what I'm going to call this slot so for example I could call this title right so now I'm passing in all of this content right here yet now if I go over to the modal and press open modal we don't actually see this right here this div with a slot of title this is an output this stuff is but this isn't and that's because we've given this a specific named slot and right here where we use the slot tags that locks for content passed into the component like this that doesn't have a specific slot anything else which does have a named slot has to be output specifically inside this component now the way we do that is by saying for example slot name so slot name is equal to and then whatever the name of the slot was in our case title so title like so and now that is going to look for a slot over here inside the modal with that slot name of title is going to grab that and it's going to output it right here everything else inside this that doesn't have a slot name is output where we have just the normal slot tags so if I save this we should see the title at the bottom and the cool thing about these named slots is it doesn't matter what order we pass them into the component it's South because we can then just access them in here and output them wherever we want for example I could do that at the top and then do a paragraph tag to say hey there and then save it and we should see the title at the top now then the paragraph tag then the form even though when we pass the content in the slot was at the bottom so these slots give us a way to essentially pass in options or different parts of contents into a component so let me now just get rid of this and let me get rid of this I don't need to use named slots in our example so I'm gonna save that and also I'm gonna take this and delete it and get rid of the comments up here so that now we're just passing in all the default content which is registered in this normal slot tag or right here so now we should see add a new person with that form so now we know about this and we can pass a form into this modal in the next video I want to talk a little bit more about forms so that we can start adding new data to this array over here
Original Description
Hey gang, in this Svelte tutorial we'll take a first look at slots - another way to pass dynamic content into another component.
🐱👤🐱👤 JOIN THE GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
----------------------------------------
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Course Files:
https://github.com/iamshaunjp/svelte-tutorial/tree/lesson-14
🐱💻 🐱💻 Other Related Free Courses:
+ HTML & CSS Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G
+ Modern JavaScript - https://www.youtube.com/playlist?list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
🐱💻 🐱💻 Svelte Docs
https://svelte.dev/tutorial/basics
🐱💻 🐱💻 The Net Ninja Community Boards:
https://community.thenetninja.co.uk/
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: HTML & CSS
View skill →Related Reads
📰
📰
📰
📰
Tags, Releases, and Branches: A Practical Guide to Frontend Deployment
Medium · Programming
Tags, Releases, and Branches: A Practical Guide to Frontend Deployment
Medium · DevOps
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Medium · Programming
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI