Slot Scope Props Vue



  1. Slot Scope Props Vue Set
  2. Slot-scope= Props Vuetify
  3. Slot Scope Props Vue Download
  4. Slot Scope Props Vueling
  5. Vue Slot-scope Props Not Defined

Vue 2.6 is released with new syntax for Slots using v-slot directive. In this tutorial, we’re gonna show you:

In these screencast, I walk through how scoped slots work in Vue.js and how thinking of them like function props can make them a lot easier to understand. Category Science & Technology. The slots syntax has been changed in Vue 2.6, the following examples use the new v-slot syntax instead of the deprecated slot-scope, but it is still supported and you can use it. However v-slot have different semantics, consult the Vue docs for more information. This rule reports deprecated slot-scope attribute in Vue.js v2.6.0. As the ultimate resource for Vue.js developers, Vue Mastery produces weekly lessons so you can learn what you need to succeed as a Vue.js Developer.

  • 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:

Slot Scope Props Vue Set

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 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:

Slot-scope= Props Vuetify

  • 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:

– do not wrap it in a <template>:

Slot scope props vue download

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!

Further reading

Isolation of Vue scope of components

These two pieces of code already have two different Vue scopes. One is to use thecurrent-userThe page of the module or the Vue instance and corresponding scope of the module, the other iscurrent-userThe module itself corresponds to the Vue instance and the corresponding scope.

These two scopes are isolated from each other. Without any additional settings, you cannot access each other’s Vue instances, so don’t say anythingdatamethodsThe members of such a Vue instance are all non interactive.

On this basis, when we need to achieve the goal that these two scopes can know some information about each other, we have to take some settings. It’s included in the setupScope slot。 At the same time, the two scopes can be considered to have parent-child relationship, which is used in the first code segmentcurrent-userThe module’s page or the module’s Vue scope is calledcurrent-userThe parent scope of the Vue scope of the module. Further, we can divide the goals into two categories: first, the Father knows the information of the son; second, the son knows the information of the father.

The real purpose of the scope slot is to let the parent know the information of the child

At first, I mistakenly thought that the purpose of the scope slot was to let the child know the parent’s information, so that I could never understand the scope slot. But if you take the idea that the purpose of the scope slot is to let the father know the son, you will feel that the official document is suddenly open. Official document portal.

Where does the user come from? So.

Props

Slot Scope Props Vue Download

Yes, this oneuserFrom the very beginningcurrent-userIn the Vue scope within this module, the section of official documents is not intended to solve the internal problem{{user.lastName}}OfuserNeed parent pass in, but in parent{{ user.firstName }}OfuserNeed the son to tell the father!

By analyzing the mapping on this graph, we can understand why the father can know the information in the child.

First of all, when using components, the father can’t write directly that he wants to replace them as comfortably as before<slot>We need to use one first<template>

Secondly, the father must know the name of the son he wants to know. If there is only one child, the child must be called “defualt”. It doesn’t matter if it’s not written in the son, or if it’s not written in the father. That is in the picture.name='default'andv-slot:default='slotProps'Of:defualtCan be deleted.

Then for the data in the child that you want to know, consider a key value for it, which isv-bind:userMediumuser, and the data value of the child corresponding to the key isv-bind:user='user'in'user'Affirmatory. This forms a key value pair of “slot prop”, or a member variable in an object containing all “slot prop”.

And then in the corresponding<slot>Of<template>On, give a name to the reference of the object containing all the “slot prop” in the formatV-slot [: slot name] = 'what you love'Here[:slotname]How to write the first paragraph and say it out? Since it’s a name, it must be something to love, as long as it’s consistent.

At this point, the real purpose and writing of the scope slot are clear. Let’s take a look at its actual use.

Actual use of scope slots

The scope slot is used by the button of the action column in the custom column template style of the table component of the elementui

Only some codes are posted here

The obsolete writing method used here. Paste the corresponding relationship between the obsolete writing method and the current recommended writing method
Still the same, if there’s only one<slot>It doesn’t matter whether you write or not.

Slot Scope Props Vueling

In this instance of elementui, you can see its use of scope slots:scope.$indexThis explains$indexyes<el-table-column>A data within the module, afterconsole.log()In the big method test, it is found that this corresponds to the number of rows in the row where the deleted button is clicked minus one.

Vue Slot-scope Props Not Defined

It’s reasonable to think so,<el-table-column>Data traversal in<el-table>Of:data=The bound array is continuously generated. Each time it is generated, there is a sequence number inside (that is, a child)$indexTo record ourselves is the number one. At this time, we need to use the scope slot to access this data like in the parent.