current position:Home>[Vue] play with the slot
[Vue] play with the slot
2022-04-29 16:33:39【Jiang Xiaopi】
Form writing habits together ! This is my participation 「 Nuggets day new plan · 4 Yuegengwen challenge 」 Of the 6 God , Click to see the event details .
stay Vue in , Slot is a very powerful function , It can greatly enhance the flexibility of encapsulated components , For example, you use a slot when encapsulating a component , When the parent component is called , The content here can be freely defined by the parent component , There is no need to think hard about how to cover various usage scenarios when encapsulating components .
Base slot
Suppose we now have a component that needs to be encapsulated SlotComponent
<template>
<div> This is a slot:<slot> </slot></div>
</template>
Copy code
<SlotComponent> Content from parent component </SlotComponent>
Copy code
stay slot
The content in can be defined arbitrarily in the parent component . If not in the component <slot>
Elements , Then when the parent component calls , The content between the start and end tags of the component will be lost .
since slot
Is in SlotComponent
In the component , Can it be used when calling in the parent component SlotComponent
The data in the component ? Obviously not , Because they are in different scopes .
Everything in the parent template is compiled in the parent scope ; Everything in the sub template is compiled in the sub scope .
Default content for slot
The slot can also set the default content , It's a bit like es6
The default value of the function parameter in , When the parent component calls, no content is provided , Then the default value will be rendered . If the content is provided , Will override the default content .
<template>
<div>
<slot> This is a slot Default content of </slot>
</div>
</template>
Copy code
<DefaultSlot></DefaultSlot>
Copy code
There is no content provided between tags at the time of invocation , The default value is to render , Become a cover story .
A named slot
If I need multiple slots in the component , Then we need to give slot
add to name
, To distinguish where the content is rendered .
// named slot, Name is NamedSlot
<template>
<div>
This is a named slot
<div>
<slot name="slot1"></slot>
</div>
<div>
<slot name="slot2"></slot>
</div>
<div>
<slot name="slot3"></slot>
</div>
</div>
</template>
// Invoke in parent component
<NamedSlot>
<template v-slot:slot1> This is insert slot1 The content of </template>
<template v-slot:slot2> This is insert slot2 The content of </template>
<template v-slot:slot3> This is insert slot3 The content of </template>
</NamedSlot>
Copy code
to slot
add name
After attribute , Can pass v-slot:slotName
Distribute content in the form of . Do not give name
Attribute words , By default name
by default
, Equate to v-slot:default
, It can also be abbreviated as #default
.
Be careful ,v-slot
Can only be added to <template>
On , But there are special cases , I'll talk about it later .
How do slots access the contents of subcomponents
adopt slot
, We can add content for child components in the parent component , But the scope of parent-child components is different , What should we do if we want to use the data of child components in the parent component ?
Can be found in sub components <slot>
Binding attribute on element , The value is what you need to pass to the parent component .
// Child components The component name is SlotProp
<div>
<slot name="slot1" :value1="child1"></slot>
<slot name="slot2" :value2="child2"></slot>
</div>
// call
<SlotProp>
<template v-slot:slot1="slotProps">
{{ slotProps.value1 }}
</template>
<template v-slot:slot2="slotProps">
{{ slotProps.value2 }}
</template>
</SlotProp>
Copy code
Simply speaking , Is in the slot
through :key='value'
Bind a value in the form of ,
When the parent component calls , adopt v-slot:slotName="slotProps"
Get this value in the form of ,slotProps
Names can be defined by themselves ,
Re pass slotProps[key]
Go get this value .
If the component has only one default template , Yes, you don't have to template
Write on v-slot:slotName="slotProps"
Of , You can write... Directly on the component name v-slot
<SlotProp v-slot:default="slotProps">
{{ slotProps.value1 }}
</SlotProp>
Copy code
As mentioned above , Not specified name
Would be considered to be default
, It's the same here ,v-slot:default="slotProps"
It can be abbreviated as v-slot="slotProps"
.
slotProps Deconstruction of
The internal working principle of the scope slot is to include the contents of your slot in a function passing in a single parameter
Implementation principle based on slot , We can also use some ES6
To operate slotProps
, such as deconstruction ,prop rename and Assign default etc. .
// deconstruction
<SlotProp v-slot="{ value1 }">
{{ value1 }}
</SlotProp>
// rename , take value1 Rename it to name1
<SlotProp v-slot="{ value1: name1 }">
{{ name1 }}
</SlotProp>
// Assign default
<SlotProp v-slot="{ value1 = ' This is the default ' }">
{{ value1 }}
</SlotProp>
Copy code
Dynamic slot name
slot
Supported by <template v-slot:[dynamicSlotName]>
This way to achieve dynamic slot .
Sometimes multiple... Are inserted in a loop in a basic component slot
, Such as this
<div v-for="item in slotList" :key="item">
<slot :name="item"></slot>
</div>
Copy code
When calling, you can use Dynamic slot name + loop To dynamically render the corresponding slot
<template v-for="item in slotList" v-slot:[item] >
{{item}}
</template>
Copy code
copyright notice
author[Jiang Xiaopi],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/04/202204291633342777.html
The sidebar is recommended
- HTTP becomes HTTPS, self-made certificate
- Web front-end operation - tourism enterprise marketing publicity responsive website template (HTML + CSS + JavaScript)
- Self inspection list of a [qualified] front-end Engineer
- This principle in JavaScript and six common usage scenarios
- JavaScript this priority
- Analyzing the principle of HTTPS encryption
- Difference and principle between websocket and http
- Use of elementui scroll bar component El scrollbar
- Nginx security optimization
- GAC group has become the first pilot enterprise of "yueyouhang". Blessed are the car owners in Guangdong!
guess what you like
Loki HTTP API usage
JavaScript - prototype, prototype chain
Front end experience
JavaScript -- Inheritance
HTTP cache
Filters usage of table in elementui
A JavaScript pit encountered by a non front-end siege lion
Grain College - image error when writing Vue with vscode
Utility gadget - get the IP address in the HTTP request
Could not fetch URL https://pypi.org/simple/pytest-html/: There was a problem confirming the ssl cer
Random recommended
- Function of host parameter in http
- Use nginx proxy node red under centos7 and realize password access
- Centos7 nginx reverse proxy TCP port
- In eclipse, an error is reported when referencing layuijs and CSS
- Front end online teacher Pink
- Learn to use PHP to insert elements at the specified position and key of the array
- Learn how to use HTML and CSS styles to overlay two pictures on another picture to achieve the effect of scanning QR code by wechat
- Learn how to use CSS to vertically center the content in Div
- Learn how to use CSS to circle numbers
- Learn to open and display PDF files in HTML web pages
- The PHP array random sorting function shuffle() randomly scrambles the order of array elements
- JQuery implements the keyboard enter search function
- 16 ArcGIS API for JavaScript 4.18 a new development method based on ES modules @ ArcGIS / core
- 17 ArcGIS API for JavaScript 4.18 draw points, lines and faces with the mouse
- 18 ArcGIS API for JavaScript 4.18 obtain the length and area after drawing line segments and surface features
- Vue environment construction -- scaffold
- Build a demo with Vue scaffold
- Using vuex in Vue projects
- Use Vue router in Vue project
- 26 [react basics-5] react hook
- 07 Chrome browser captures hover element style
- WebGIS development training (ArcGIS API for JavaScript)
- Solution to the blank page of the project created by create react app after packaging
- 19. Html2canvas implements ArcGIS API for JavaScript 4 X screenshot function
- Introduction to JavaScript API for ArcGIS 13
- Development of ArcGIS API for JavaScript under mainstream front-end framework
- Nginx learning notes
- Less learning notes tutorial
- Vue2 cannot get the value in props in the data of the child component, or it is always the default value (the value of the parent component comes from asynchrony)
- LeetCode 217. Determine whether there are duplicate elements in the array
- I'll write a website collection station myself. Do you think it's ok? [HTML + CSS + JS] Tan Zi
- Front end browser debugging tips
- Application of anti chattering and throttling in JavaScript
- How to create JavaScript custom events
- Several ways of hiding elements in CSS
- node. Js-3 step out the use of a server and express package
- CSS matrix function
- Fastapi series - synchronous and asynchronous mutual conversion processing practice
- How to extend the functionality of Axios without interceptors
- Read pseudo classes and pseudo elements