This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-作用域插槽的用法.html
62 lines (59 loc) · 1.6 KB
/
11-作用域插槽的用法.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="./lib/vue.js"></script>
<style>
.current {
color: red;
}
</style>
</head>
<body>
<div id="app">
<fruit-list :fruitlist="fruitlist">
<template slot-scope="slotProps">
<span class="current" v-if="slotProps.info.id === 3">
{{slotProps.info.name}}
</span>
<span v-else>{{slotProps.info.name}}</span>
</template>
</fruit-list>
</div>
<script>
// 作用域插槽应用场景:父组件对子组件的内容进行加工处理
// slot就是放置组件插槽的
Vue.component("fruit-list", {
props: ["fruitlist"],
template: `
<ul>
<li :key="item.id" v-for="item in fruitlist">
<slot :info="item">
{{item.name}}
</slot>
</li>
</ul>
`,
});
const vm = new Vue({
el: "#app",
data: {
fruitlist: [{
id: 1,
name: "apple",
}, {
id: 2,
name: "orange",
}, {
id: 3,
name: "bannana",
}, ],
},
methods: {},
});
</script>
</body>
</html>