vue component slot

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<html>
<header>
<title>Vue.js slot</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</header>

<body>
<div id="box">
<sidebar1>
<button @click="isShow=!isShow">侧边栏</button>
</sidebar1>
<sidebar2 v-show="isShow">
<div slot="a">slot A </div>

<ul slot="a">
<li v-for="data in datalist"> {{data}} </li>

</ul>
<div slot="b">slot B </div>
</sidebar2>
</div>
</body>

<script>


Vue.component('sidebar1', {
template: `<div>
<slot></slot>
</div>`,
})

Vue.component('sidebar2', {
template: `<div style="background:red;width:200px">

<slot name="a"></slot>
<slot name="b"></slot>
</div>`
})

var vm = new Vue({
el: "#box",
data: {
isShow: false,
datalist: [1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 0000]
},

})
</script>

</html>