No slots<div id="app"> <child> <span>1111</span> </child> </div> <script> // Register child component Vue.component("child", { template: "<div>This is a div tag</div>" }); // Initialize the parent component new Vue({ el: "#app" }); </script>
Vue2.x SlotsWith slots In simple terms, using <div id="app"> <child> <span>1111</span> </child> </div> <script> // Register child component Vue.component("child", { template: "<div>This is <slot></slot>a div tag</div>" }); // Initialize the parent component new Vue({ el: "#app" }); </script> Even if there are multiple tags, they will be inserted together, which is equivalent to replacing the <div id="app"> <child> <span>1111</span> <i>2222</i> <b>3333</b> </child> </div> <script> // Register child component Vue.component("child", { template: "<div>This is <slot></slot>a div tag</div>" }); // Initialize the parent component new Vue({ el: "#app" }); </script> Named Slots
<div id="app"> <child> <span slot="one">1111</span> <i slot="two">2222</i> <b slot="three">3333</b> </child> </div> <script> // Register child component Vue.component("child", { template: `<div> This is <slot name='one'></slot> One <slot name='two'></slot> div <slot name='three'></slot> Tags </div>` }); // Initialize the parent component new Vue({ el: "#app" }); </script> No slot attributeIf there is no slot attribute in the subcomponent tag, the default value will be displayed <div id="app"> <child> <!-- <span slot="one">1111</span> --> <i slot="two">2222</i> <!-- <b slot="three">3333</b> --> </child> </div> <script> // Register child component Vue.component("child", { template: `<div> <slot name='one'>no one</slot> <slot name='two'>no two</slot> <slot name='three'>no three</slot> </div>` }); // Initialize the parent component new Vue({ el: "#app" }); </script>
Slot simple example applicationThink about the various slots on your computer motherboard. Some are for the CPU, some are for the graphics card, some are for the memory, and some are for the hard disk. So suppose there is a component called computer, and its template is template, as follows: <body> <div id="app"> <computer> <div slot="CPU">Intel Core i7</div> <div slot="GPU">GTX980Ti</div> <div slot="Memory">Kingston 32G</div> <div slot="Hard-drive">Samsung SSD 1T</div> </computer> </div> <script> // Register subcomponent Vue.component("computer", { template: `<div> <slot name="CPU">Plug in your CPU here</slot> <slot name="GPU">Plug in your graphics card here</slot> <slot name="Memory">Insert your memory here</slot> <slot name="Hard-drive">Plug your hard drive here</slot> </div>` }); // Initialize the parent component new Vue({ el: "#app" }); </script> </body> Scoped slots (new in 2.1.0)A scoped slot is a special type of slot that acts as a reusable template (that can be passed data) to replace an already rendered element.
<div id="app"> <child> <!-- 2. Receive myName data, scoped to { "myName": "猫老板的豆" } --> <template slot="content" slot-scope="scoped"> <div>{{ scoped.myName }}</div> </template> </child> </div> <script> Vue.component('child', { data () { return { myName: 'Cat Boss's Beans' } }, template: `<slot name="content" :myName="myName"></slot>` // 1. Throw out myName data }) new Vue({ el: "#app" }); </script> Vue3.x SlotsSlots<!-- Parent component --> <template> <Child> <!-- Vue2.x writing <div slot="parent"> <div>parent component</div> </div> --> <template v-slot:parent> <div>parent component</div> </template> </Child> </template> <!-- Child Component --> <template> <slot name='parent'>child component</slot> </template> Scoped Slots In Vue2.x, named slots and scoped slots are implemented using Parent component: <template> <Child> <!-- <template slot="content" slot-scope="scoped"> --> <template v-slot:content="scoped"> <div>{{scoped.myName}}</div> </template> </Child> </template> Subcomponents: <template> <slot name="content" :myName="myName"></slot> </template> <script> import { ref } from 'vue' export default { setup () { let myName = ref("Mr. Cat's Bean") return { myName } }, } </script> This is the end of this article about the detailed usage of scoped slots of Vue.js slots. For more content related to scoped slots of Vue.js slots, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: mysql solves time zone related problems
>>: CUDA8.0 and CUDA9.0 coexist under Ubuntu16.04
This article example shares the specific code of ...
Why did I use this? It all started with the makin...
There is a picture in a big box. When you put the...
This article shares with you a draggable photo wa...
This article shares the specific code for impleme...
Enter the running container # Enter the container...
Table of contents Introduction effect principle f...
Running Docker requires root privileges. To solve...
1. It is best to add a sentence like this before t...
Table of contents 1. The role of nginx process lo...
Find the problem Let's look at the problem fi...
If you want to wrap the text in the textarea input...
Table of contents 1: Prepare https certificate 2:...
The following is some basic sql knowledge I have ...
This article will introduce some commonly used ar...