1. Introduction to v-slot Example of use: //Parent component code <child-com> <template v-slot:nameSlot> Slot content</template> </child-com> //Component template <slot name="nameSlot"></slot> The syntax of v-slot simplifies the functions of slot and slot-scope scope slots, making them more powerful and more efficient. 2. Anonymous Slots When there is only one slot in a component, Anonymous slot usage: //Component call <child-com> <template v-slot> Slot content</template> </child-com> //Component template <slot ></slot> Although 3. Named Slots When there are multiple slots in a component, if the v-slot attribute value is not set, the element will be inserted into the slot component without a name attribute value by default. In order to put the corresponding element in the specified position, you need to use Named slots use: //Parent component <child-com> <template v-slot:header> Header</template> <template v-slot:body> Contents</template> <template v-slot:footer> feet </child-com> //Subcomponent <div> <slot name="header"></slot> <slot name="body"></slot> <slot name="footer"></slot> </div> Named slot abbreviations: As mentioned above, v-slot:footer can be shortened to #footer. The above parent component code can be simplified to: <child-com> <template #header> Header</template> <template #body> Contents</template> <template #footer> feet </child-com>
4. Scoped SlotsSometimes it is useful to give slot content access to data that is only available in child components. It's a common case when a component is used to render an array of items, and we want to be able to customize how each item is rendered. To make properties on the child component available on the slot content, you need to bind a property to Example of use: // <child-com> <template v-slot:header="slotProps"> Slot content --{{ slotProps.item }} Serial number --{{ slotProps.index }} </template> </child-com> //Subcomponent code <template> <div v-for="(item, index) in arr" :key="index"> <slot :item="item" name="header" :index="index"></slot> </div> </template> <script setup> const arr = ['1111', '2222', '3333'] </script> 5. Dynamic slot names like: <child-com> <template v-slot:[dd()]> Dynamic slot name</template> </child-com> <script setup> const dd = () => { return 'hre' } Functions are used here, but variables can also be used directly. This is the end of this article about the summary of Vue3 slot usage. For more relevant Vue3 slot usage content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to draw a vertical line between two div tags in HTML
>>: Pure CSS3 code to implement a running clock
Table of contents mysql filtered replication Impl...
Table of contents 1. Introduction to built-in obj...
Table of contents Preface Initialize the project ...
WeChat applet: Simple calculator, for your refere...
1. Delete folders Example: rm -rf /usr/java The /...
First, pull the image (or just create a container...
Study plans are easily interrupted and difficult ...
Table of contents Preface analyze Data Total Repe...
Create a test table -- --------------------------...
Table of contents 1. Falling into the pit 2. Stru...
Table of contents 1. props/$emit Introduction Cod...
1. Record several methods of centering the box: 1...
This article shares with you a detailed tutorial ...
<br />In previous tutorials of 123WORDPRESS....
This article shares the specific code of Vue to a...