1. Why use slots?1.1 slot
1.2 Slots in Components
1.3 Examples
2. How to encapsulate such components (slot)
3. Slot Case<div id="app"> <cpn><button>Button</button></cpn> <cpn><p>hello world</p></cpn> <cpn><p>666</p></cpn> </div> <template id="cpn"> <div> <h2>I am a component</h2> // The slot is reserved for users to fill in <slot></slot> </div> </template> <script src="../js/vue.js"></script> <script> const app = new Vue({ el: "#app", components: "cpn": { template: `#cpn`, } } }) </script> The above code does the following:
The final display effect is as follows: 4. Slot default valuesIf we need to use this component a lot, and most of the slots reserved by the component are filled with return buttons, and only a few are filled with other ones, then in this case you can set a default value for the slot <div id="app"> <cpn></cpn> <cpn></cpn> <cpn></cpn> </div> <template id="cpn"> <div> <slot><button>Return</button></slot> </div> </template> We set a slot with a default value of the back button in the child component. If the parent component does not fill in anything when it is used, the default is the back button. 5. Named SlotsSometimes we need more than one slot. For example, for a component with the following template: <template id="cpn"> <div> <span>Header</span></slot> <span>Middle</span></slot> <span>Footer</span></slot> </div> </template> We have reserved three slots in the component, but here we specify three names. The subsequent parent component can fill in its own content after using <div id="app"> <cpn> <template v-slot:header> <p>header</p> </template> <template v-slot:footer> <p>footer</p> </template> </cpn> </div>
6. Compilation scopeVariables passed to the component from outside cannot be used when the slot is used later <div id="app"> <cpn v-show="isShow"></cpn> </div> <template id="cpn"> <p>hello</p> </template> <script src="../js/vue.js"></script> <script> const app = new Vue({ el: "#app", data: { isShow: true }, components: "cpn": { template: `#cpn`, data(){ return { isShow: false } } } } }) </script> Above we defined the subcomponent 7. Scoped Slots By default, the code in the slot can only use the properties in the global <div id="app"> <cpn> <template v-slot:default="slot"> {{slot.data.firstName}} </template> </cpn> </div> <template id="cpn"> <div> <slot :data="user"> {{user.lastname}} </slot> </div> </template> <script src="../js/vue.js"></script> <script> const app = new Vue({ el: "#app", components: "cpn": { template: `#cpn`, data(){ return { "user": { "firstName": "甲", "lastname": "shellworm" } } } } } }) </script> The above code does the following things:
This is the end of this article about the details of using Vue slot. For more information about the usage of Vue slot, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: CSS Sticky Footer Implementation Code
>>: A brief description of the relationship between k8s and Docker
This tutorial explains how to verify the IP addre...
Relative Length Units em Description: Relative len...
1. Introduction to Flex Layout Flex is the abbrev...
Table of contents Preface Static scope vs. dynami...
1. Introduction This article will show you how to...
If you open some Microsoft documents with LibreOf...
This article mainly introduces the layout method ...
Preface The best method may not be the one you ca...
1. Text formatting: This example demonstrates how...
Table of contents background Question 1 Error 2 E...
1. Problem description Due to some reasons, the d...
CEP - Complex Event Processing. The payment has n...
The code can be further streamlined, but due to t...
What is volume? Volume means capacity in English,...
There is a new build tool in the Vue ecosystem ca...