vue递归实现自定义tree组件
本文实例为大家分享了vue递归实现自定义tree组件的具体代码,供大家参考,具体内容如下
1. 在tree/index.vue中:
<template>
<div>
<ul>
<item :model='data'></item>
</ul>
</div>
</template>
<script>
import Item from './item'
export default {
components:{
Item
},
data(){
return{
data:{
title:"一级1",
children:[
{
title:"二级1-1",
children:[
{
title:"三级1-1-1",
children:[
{
title:"四级1-1-1-1",
children:[
{
title:"五级1-1-1-1-1"
}
]
}
]
}
]
},{
title:'二级1-2',
children:[
{
title:"三级1-2-1"
}
]
}
]
}
}
}
}
</script>
2. item.vue组件:
<template>
<li>
<div @click="toggle">
{{model.title}}
<span v-if="isFolder">[{{open?'-':'+'}}]</span>
</div>
<ul v-show="open" v-if="isFolder">
<item v-for="(child,index) in model.children" :model='child' :key='index'></item>
</ul>
</li>
</template>
<script>
export default {
name:'Item',
props:{
model:{
type:Object,
required:true
}
},
data(){
return{
open:false
}
},
computed:{
isFolder(){
return this.model.children && this.model.children.length>0
}
},
methods:{
toggle(){
if(this.isFolder) this.open =!this.open
}
}
}
</script>
3. 在任意组件中使用:
<template>
<div class="index">
<Tree></Tree>
</div>
</template>
<script>
import Tree from "@/components/tree"
components:{
Tree
}
</script>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
以上是 vue递归实现自定义tree组件 的全部内容, 来源链接: utcz.com/p/239588.html