-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.插槽.html
72 lines (70 loc) · 1.46 KB
/
13.插槽.html
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>插槽</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<div id="app">
<!-- 只要出现多个插槽,请始终为所有的插槽使用完整的基于 <template> 的语法 -->
<slot-comp>
<!-- 带名字的一定要包裹在template元素中 -->
<template v-slot:header>
头
</template>
身体
<!-- 缩写 -->
<template #footer>脚</template>
</slot-comp>
<slot-prop>
<!-- 父组件访问子组件内容,名字随意 -->
<!-- 如果写成v-slot="ceshi",就是使用默认插槽 -->
<template v-slot:default="ceshi">
{{ceshi.user.lastName}}
</template>
<!-- 第二种写法 -->
<!--
<template v-slot:default="{user}">
{{user.lastName}}
</template>
-->
</slot-prop>
</div>
</body>
<script type="text/javascript">
var slotComp = {
template:`
<div>
<header><slot name="header"/></header>
<!-- 不带名字的,有个默认名字default -->
<main><slot/></main>
<footer><slot name="footer"/></footer>
</div>
`
}
var slotProp = {
data(){
return {
user:{
firstName:'孙',
lastName:'茂林'
}
}
},
template:`
<div>
<slot :user="user">{{user.firstName}}</slot>
</div>
`
}
var vm = new Vue({
el:'#app',
components:{
slotComp,slotProp
},
data:{
}
})
</script>
</html>