软糖

Vue2-2 component

Simple Component

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html>
<body>
<div id="root">
<task></task>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="./order.js"></script>
</body>
</html>
1
2
3
4
5
6
7
Vue.component('task', {
template: '<li>Foobar</li>'
})
new Vue({
el: '#root',
})

Sub component

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html>
<body>
<div id="root">
<task-list title="my todo list"></task-list>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="./order.js"></script>
</body>
</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
Vue.component('task-list', {
props: ['title'],
template: `
<div v-show="isVisible">
<div>{{ title }}</div>
<task v-for="t in tasks">{{ t.task }}</task>
<button type="button" @click="hideModel">close</button>
</div>
`,
data() {
return {
isVisible: true,
tasks: [
{ task: 'Go to the store', complete: true },
{ task: 'Go to the email', complete: false },
]
}
},
methods: {
hideModel() {
this.isVisible = false
}
},
})
Vue.component('task', {
template: '<li><slot></slot></li>',
})
new Vue({
el: '#root',
})