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
a href="#"> After clicking the link, ...
The default operating mode of MySQL is autocommit...
I am currently learning MySQL. I am a complete no...
Anyone in need can refer to it. If you have tried...
IIS7 needs to confirm whether the "URL REWRI...
Table of contents need: drive: Ideas: accomplish:...
Preface: I have always wanted to know how a SQL s...
When the Docker container exits, the file system ...
This article shares the specific code for JavaScr...
The code looks like this: <!DOCTYPE html> &...
Table of contents Overview Hash Properties Host p...
This article shares the installation tutorial of ...
First put a piece of code for(int i=0;i<1000;i...
Table of contents Congruent and Incongruent congr...
In the vertical direction, you can set the alignm...