-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathitem-template.vue
57 lines (57 loc) · 1.4 KB
/
item-template.vue
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
<template>
<li>
<div :class="{bold: isFolder}" @click="toggle" @dblclick="changeType">
{{model.name}}
<span v-if="isFolder">[{{open ? '-' : '+'}}]</span>
</div>
<ul v-show="open" v-if="isFolder">
<item class="item" v-for="model in model.children" :model="model" >
</item>
<li class="add" @click="addChild">+</li>
</ul>
</li>
</template>
<script>
import Vue from 'vue' // 递归组件, 每次都要产生 vue 的实例,这里要加上独立的 vue
export default {
name: 'item', // 递归组件,要加上 name
props: {
model: Object
},
data: function() {
return {
open: false
}
},
computed: {
isFolder: function() {
return this.model.children &&
this.model.children.length
}
},
methods: {
toggle: function() {
if (this.isFolder) {
this.open = !this.open
}
},
changeType: function() {
if (!this.isFolder) {
Vue.set(this.model, 'children', [])
this.addChild()
this.open = true
}
},
addChild: function() {
this.model.children.push({
name: 'new stuff'
})
}
}
}
</script>
<style>
.example {
color: red;
}
</style>