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>
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。