Vue Js Slots Tutorial
Vue.js simplified: components, props, and slots December 10, 2020 8 min read 2425 In this article, we will be using Vue.js, Vue CLI and Bootstrap CSS. Get your learning on with Reusable Vue.js Components with Slots Get access to the most comprehensive Vue.js video library in the world. Over 300 video lessons including the newest Vue 3 features. In order to use multiple slots in vue, vuejs provides us with a way to name our slots. What if we want the h2 and h3 tags in the parent component to be displayed individually in separate slots. Vue.js - The Progressive JavaScript Framework. This change unifies normal and scoped slots in 3.x. Here is a quick summary of what has changed.
Remarks
Vue.js
is a rapidly growing front-end framework for JavaScript
, inspired by Angular.js
, Reactive.js
, and Rivets.js
that offers simplistic user-interface design, manipulation, and deep reactivity.
It is described as a MVVM
patterned framework, Model-View View-Model
, which is based on the concept of two-way binding data to components and views. It is incredibly fast, exceeding speeds of other top-tier JS
frameworks, and very user friendly for easy integration and prototyping.
Versions
Version | Release Date |
---|---|
2.4.1 | 2017-07-13 |
2.3.4 | 2017-06-08 |
2.3.3 | 2017-05-09 |
2.2.6 | 2017-03-26 |
2.0.0 | 2016-10-02 |
1.0.26 | 2016-06-28 |
1.0.0 | 2015-10-26 |
0.12.0 | 2015-06-12 |
0.11.0 | 2014-11-06 |
'Hello, World!' Program
To start using Vue.js, make sure you have the script file included in your HTML. For example, add the following to your HTML.
HTML template
JavaScript
See a live demo of this example.
You might also want to check out the 'Hello World' example made by Vue.js.
Handling User Input
VueJS can be used to easily handle user input as well, and the two way binding using v-model makes it really easy to change data easily.
HTML :
JS :
It is very easy to do a two-way binding in VueJS using v-model
directive.
Check out a live example here.
Hello World in Vue 2 (The JSX way)
JSX is not meant to be interpreted by the browser. It must be first transpiled into standard Javascript. To use JSX you need to install the plugin for babel babel-plugin-transform-vue-JSX
Run the Command below:
and add it to your .babelrc
like this:
Sample code with VUE JSX:
By using JSX you can write concise HTML/XML-like structures in the same file as you write JavaScript code.
Congratulations, You're Done :)
Vue 2.6 is released with new syntax for Slots using v-slot
directive. In this tutorial, we’re gonna show you:
- 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 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
:
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 #
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 Js Tutorial Point
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
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:
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:
Vue Js Slots Tutorial Free
– 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 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!