A component can be 100% responsible for generating its output, like in this case:
or it can also let the parent component inject any kind of content into it, using slots.
Slots are a mechanism for Vue components that allows you to compose your components in a way other than the strict parent-child relationship. Slots give you an outlet to place content in new places or make components more generic. The best way to understand them is to see them in action. Let’s start with a simple example. In the next parts, we show you some examples that apply new Vue v-slot directive in practice. Vue v-slot examples with Named Slots. If we want to use multiple slots in one component, Named Slots are useful. The code below shows BkrCard component template with 3 slots: header; title; default.
What is a slot? It’s a space in your component output that is reserved, waiting to be filled.
- This page assumes you've already read the Components Basics.Read that first if you are new to components. # Slot Content Vue implements a content distribution API inspired by the Web Components spec draft (opens new window), using the slot element to serve as distribution outlets for content.
- Cupertino Pane for Vue 3. A Vue 3 Wrapper for Cupertino Library. Actually kinda easy. Npm i -D v-cupertino How works Example. The component just have one simple slot where you can easily put one or multiple components (by wrapping it in a template or wrap element like a div), doesn't have any special v-slots, any component or html element used will fallback into v-slot:default.
- Apr 21, 2020 In this tutorial, we will learn about how to use the slots in vue.js with the help of examples. Slots helps us to pass the data between opening and closing component tags. In vue.js props are used to pass the data to its child components, but it is hard to pass when we have a complex code. In such cases slots can be used.
You define a slot by putting <slot></slot>
in a component template:
When using this component, any content added between the opening and closing tag will be added inside the slot placeholder:
If you put any content side the <slot></slot>
tags, that serves as the default content in case nothing is passed in.
Slot-scope Vue Example
A complicated component layout might require a better way to organize content, with multiple slots as well.
This is why Vue offers us named slots.
Named slots
With a named slot you can assign parts of a slot to a specific position in your component template layout, and you use a slot
attribute to any tag, to assign content to that slot.
Anything outside any template tag is added to the main slot
.
For convenience I use a page
single file component in this example:
Here is how we can use it, providing the slots content, in a parent component:
There is a handy shorthand, #
:
Note: Vue 2.6 deprecated the slot
attribute in favor of v-slot
, and requires it to be added to a template
tag (while slot
could be applied to any tag)
Scoped slots
In a slot, we can’t access the data contained in the child component from the parent.
Vue recognizes this use case and provides us a way to do so:
In the parent we can access the dog name we passed using:
slotProps
is just a variable we used to access the props we passed. You can also avoid setting a variable just to hold the props you pass to the child component, by destructuring the object on the fly:
Comments are closed.