Slot Scope Props Vue
Isolation of Vue scope of components
These two pieces of code already have two different Vue scopes. One is to use thecurrent-user
The page of the module or the Vue instance and corresponding scope of the module, the other iscurrent-user
The module itself corresponds to the Vue instance and the corresponding scope.
- As you can see, this looks very similar to the render prop in our React component. Here, we are returning a default scoped slot, which passes along an object with whatever we want. Here, we give it the results and our search method. In our autocomplete component, we use the slot-scope attribute to get access to the data from the child component.
- 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.
Slot-scope= Props Vuetify
These two scopes are isolated from each other. Without any additional settings, you cannot access each other’s Vue instances, so don’t say anythingdata
,methods
The members of such a Vue instance are all non interactive.
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. Receive exposed props in the parent component using slot-scope attribute. To receive the props in the parent component we declare a template element and use slot-scope attribute. The attribute has access to the object carrying all props exposed from the child component.
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-user
The module’s page or the module’s Vue scope is calledcurrent-user
The 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.
Yes, this oneuser
From the very beginningcurrent-user
In the Vue scope within this module, the section of official documents is not intended to solve the internal problem{{user.lastName}}
Ofuser
Need parent pass in, but in parent{{ user.firstName }}
Ofuser
Need 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:defualt
Can be deleted.
Then for the data in the child that you want to know, consider a key value for it, which isv-bind:user
Mediumuser
, 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
Vue Slot-scope Props Not Defined
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.$index
This explains$index
yes<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.
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)$index
To record ourselves is the number one. At this time, we need to use the scope slot to access this data like in the parent.