- Slot Attributes Are Deprecated
- Vue Named Slots
- Slot Scope Vue
- Vue Slot Event
- Template V Slot
- Slots Vue 3.5
- Vue 3.0 Slots
Vue 2.6 is released with new syntax for Slots using
v-slot
directive. In this tutorial, we’re gonna show you:Vue 3 tutorial Learning Vue 3. As the third version will have the same API, it should be relatively easy to learn it, if you're already familiar with previous versions. You'll find the Vue 3 step-by-step tutorial under this link when it's out. You can also join the newsletter above and we'll notify you when documentation and tutorials for Vue 3. Slots Vue 3 exclusive no deposit bonuses from some of the best casinos around for US players. The bonus codes on our site are unique, which means you can use them only if you get to the casino through our website.
- Syntax to use Vue Slot with
v-slot
directive along with its shorthand - How to use Vue Named Slots with
v-slot
& examples - How to use Vue
v-slot
for Scoped Slots & examples - Vue Dynamic slots example
Related Post: Vue 3 Composition API tutorial with examples
Contents
Vue slots syntax with v-slot directive
With new
– combine html layers: component tag and scope of the slot.
– combine the slot and the scoped slot in a single directive.
v-slot
directive, we can:– combine html layers: component tag and scope of the slot.
– combine the slot and the scoped slot in a single directive.
For example, this is old syntax with
slot-scope
:Slot Attributes Are Deprecated
This is how we combine
ListComponent
and template
tag:And this is old named slots syntax:
Now we use new Vue
v-slot
directive:You can see that:
– We use
<template v-slot:header>
to wrap <p>
tag instead of <p slot='header'>
directly. This is because Vue v-slot
can only be used in <component>
or <template>
html tag. It cannot be used in plain HTML tags (<p>
for example).– We replace
slot='content' slot-scope='{data}'
with v-slot:content='{data}'
by combining slot
& slot-scope
. With new Vue v-slot
directive, all slots are compiled into scoped slots. It improves the performance. Why?Normal slots are rendered during the parent component’s render cycle. So, if any dependency of a slot changes, both the parent and child components will be re-rendered.
When we use scoped slots, slots are compiled into inline functions and called during the child component’s render cycle. This means:
- data from a scoped slot are collected by the child component which is re-rendered separately.
- the changes of parent scope dependency only affect the parent, not the child component. So the child component doesn’t need to update if it uses only scoped slots.
Shorthand for v-slot
#
is the shorthand for Vue v-slot
directive.For example,
#content
stands for v-slot:content
.The code above can be written as:
Remember that when using shorthand, we must always specify the name of the slot after
It must be:
#
symbol. We cannot use shorthand like this: #='{item}'
.It must be:
#default='{item}'
in which, #default
is the shorthand for v-slot:default
.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
The code below shows
BkrCard
component template with 3 slots:- header
- title
- default
Vue Named Slots
Remember that
<slot>
without name
attribute has the name default
.Now look at the parent component which use
v-slot
directive to specify name for named slots on <template>
tag:Slot Scope Vue
The result will be:
If we pass only one named slot, the default value will be shown:
Vue v-slot example with default slot
In the example above, we use
<template v-slot:default>
for the default slot.We have other ways to specify html code to be considered as default slot also:
– wrap it in a
<template>
without Vue v-slot
directive:– do not wrap it in a
<template>
:The result are the same for 2 cases:
Vue v-slot examples with Scoped Slots
What we should do when we want a child component to allow parent component access its data?
In this example,
categories
need to be available to the slot content in the parent. So we bind the categories
as an attribute to the <slot>
element:The
In the parent scope, Vue
categories
attribute is called slot props.In the parent scope, Vue
v-slot
directive can help us get value of the slot props above:The result will be:
- Dart
- Flutter
- Vue.js
This is shorthand for
v-slot
:Vue Dynamic slots example
We can use a JavaScript expression in
v-slot
directive argument with square brackets:Now look at the example:
Clicking on the Change button will change the
collection
value dynamically.v-slot:[collection]='{categories}'
could become:v-slot:default='{categories}'
v-slot:new_categories='{categories}'
This is
BkrCategories
component with default
and new_categories
slot name:The result will be:
Conclusion
We’ve learned almost aspects of new Vue
v-slot
directive, from v-slot
syntax to its handshort, then apply v-slot
directive on Named Slot examples to Scoped Slots and Dynamic Slots examples.Happy learning! See you again!
Further reading
Tutorial
While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the 'report an issue' button at the bottom of the tutorial.
While basic component slots are all fine and dandy, sometimes you want the template inside the slot to be able to access data from the child component hosting the slot content. For example, if you’re trying to allow custom templates in a container while still retaining access to those containers’ data properties, you’ll want to use a scoped slot.
Introduction
Scoped slots are a new feature introduced in Vue 2.1.0. They allow you to pass properties from your child components into a scoped slot, and access them from the parent. Sort of like reverse property passing.
The first step to creating a scoped component slot is to pass properties into a default or named slot from your child component, like so:
Vue Slot Event
Then, to use those properties inside a parent component’s slot content, create a template element tied to the slot. Add a scope attribute to the template element and set it to the name that you wish to access the scope properties from. This essentially creates a local variable for anything inside that template, allowing you to access it as if it was in the parent’s scope.
Template V Slot
(For example, scope=“myScope”, properties passed into the <slot> will be accessible as {{myScope.myProperty}} while scope=“yourScope” will be accessed as {{yourScope.myProperty}}.)
Note: The template element does not get added to the DOM. It’s simply a wrapper.
Slots Vue 3.5
ParentComponent.vue
Vue 3.0 Slots
If you’re using
Vue 2.5
or above, use the slot-scope
attribute instead of scope
. Also you can skip the template elements.