VUE 無(wú)限層級(jí)樹(shù)形數(shù)據(jù)結(jié)構(gòu)顯示的實(shí)現(xiàn)
在做項(xiàng)目中,會(huì)遇到一些樹(shù)形的數(shù)據(jù)結(jié)構(gòu),常用在左側(cè)菜單導(dǎo)航,或者評(píng)論引用等地方,這種數(shù)據(jù)結(jié)構(gòu)有個(gè)特點(diǎn)是不知道它會(huì)嵌套多少層,所以用template去展示這樣的數(shù)據(jù)時(shí)就有點(diǎn)棘手,這篇文章梳理兩種展示這種數(shù)據(jù)結(jié)構(gòu)的方法。
文章中用到的數(shù)據(jù)是下面這個(gè):
mainData: {
value: "root",
children:[{
value: "層級(jí)1-1",
children:[{
value: "層級(jí)2-1",
children:[{
value: "層級(jí)3-1",
children:[]
}]
},{
value: "層級(jí)2-2",
children:[]
}]
},{
value: "層級(jí)1-2",
children:[]
}]
}
也就是下面這個(gè)樣子。

組件遞歸調(diào)用
第一種是組件遞歸調(diào)用自己的方式,創(chuàng)建一個(gè)組件,該組件在引用自己去展示children的數(shù)據(jù),子組件如下:
<template>
<div>
<div class="demo">
{{treeData.value}}
<tree-comp v-for="(item, index) in treeData.children" :treeData="item"></tree-comp>
</div>
</div>
</template>
<script>
export default {
name: 'treeComp',
props:{
treeData: {
default: function(){
return {}
}
}
},
mounted(){},
methods:{}
}
</script>
<style lang="less" scoped>
.demo{padding:5px 0;margin:1px 10px;text-align: left;font-size:16px;max-width:500px;border-left:1px dashed #999;
&:before{content:'--';display: inline-block;padding:0 4px;}
}
</style>
然后創(chuàng)建父組件,父組件使用子組件,并將數(shù)據(jù)傳入子組件。
<template>
<tree-comp :treeData="mainData"></tree-comp>
</template>
<script>
export default {
name: 'treeMain',
data () {
return {
mainData: {
value: "root",
children:[
{
value: "層級(jí)1-1",
children:[{
value: "層級(jí)2-1",
children:[{
value: "層級(jí)3-1",
children:[]
}]
},{
value: "層級(jí)2-2",
children:[]
}]
},{
value: "層級(jí)1-2",
children:[]
}
]
}
}
},
components:{
"tree-comp": () => import('./TreeComp')
},
mounted(){},
methods:{}
}
</script>
關(guān)于遞歸組件的內(nèi)容,在官方文檔里是有提到的-->遞歸組件
使用render方法
除了使用組件的方式,也可以使用vue的render方法,去利用JavaScript 的完全編程的能力,實(shí)現(xiàn)遞歸處理樹(shù)形數(shù)據(jù),從而展示出無(wú)限層級(jí)樹(shù)。如下:
<template>
<tree-comp :treeData="mainData"></tree-comp>
</template>
<script>
export default {
name: 'treeRender',
data () {
return {
mainData: {
value: "root",
children:[
{
value: "層級(jí)1-1",
children:[{
value: "層級(jí)2-1",
children:[{
value: "層級(jí)3-1",
children:[]
}]
},{
value: "層級(jí)2-2",
children:[]
}]
},{
value: "層級(jí)1-2",
children:[]
}
]
}
}
},
components:{
treeComp:{
functional: true,
props: {treeData: Object},
render(h, {props: {treeData = {}}}) {
const creatNode = (node)=>{
if(node.children && node.children.length > 0){
let hArr = node.children.map(item=>{
return creatNode(item)
})
return h('div', {class:'demo'}, [node.value, hArr])
}else{
return h('div', {class:'demo'}, [node.value])
}
}
return creatNode(treeData)
},
}
},
mounted(){},
methods:{}
}
</script>
<style lang="less" scoped>
.demo{padding:5px 0;margin:1px 10px;text-align: left;font-size:16px;max-width:500px;border-left:1px dashed #999;
&:before{content:'--';display: inline-block;padding:0 4px;}
}
</style>
其中最核心的就是在render方法里,creatNode方法用遞歸的方式,深度優(yōu)先遍歷樹(shù)狀數(shù)據(jù),生成vnode,然后渲染出了頁(yè)面。
到此這篇關(guān)于VUE 無(wú)限層級(jí)樹(shù)形數(shù)據(jù)結(jié)構(gòu)顯示的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)VUE 無(wú)限層級(jí)樹(shù)形結(jié)構(gòu)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue項(xiàng)目實(shí)現(xiàn)簡(jiǎn)單的權(quán)限控制管理功能
這篇文章主要介紹了Vue項(xiàng)目實(shí)現(xiàn)簡(jiǎn)單的權(quán)限控制功能,文中給大家介紹了兩種方式進(jìn)行權(quán)限限制,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-07-07
Vue項(xiàng)目中實(shí)現(xiàn)帶參跳轉(zhuǎn)功能
最近做了一個(gè)手機(jī)端系統(tǒng),其中遇到了父頁(yè)面需要攜帶參數(shù)跳轉(zhuǎn)至子頁(yè)面的問(wèn)題,現(xiàn)已解決,下面分享一下實(shí)現(xiàn)過(guò)程,感興趣的朋友一起看看吧2021-04-04
Vue中使用this.$set()如何新增數(shù)據(jù),更新視圖
這篇文章主要介紹了Vue中使用this.$set()實(shí)現(xiàn)新增數(shù)據(jù),更新視圖方式。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue-router清除url地址欄路由參數(shù)的操作代碼
這篇文章主要介紹了vue-router清除url地址欄路由參數(shù),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2015-11-11
vue3如何使用postcss-px-to-viewport適配屏幕
這篇文章主要介紹了vue3如何使用postcss-px-to-viewport適配屏幕問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
vue中前端如何實(shí)現(xiàn)pdf預(yù)覽功能(含vue-pdf插件用法)
這篇文章主要給大家介紹了vue中前端如何實(shí)現(xiàn)pdf預(yù)覽功能的相關(guān)資料,文中包含vue-pdf插件用法,在前端開(kāi)發(fā)中,很多時(shí)候我們需要進(jìn)行pdf文件的預(yù)覽操作,需要的朋友可以參考下2023-07-07
關(guān)于Element-UI中slot的用法及說(shuō)明
這篇文章主要介紹了關(guān)于Element-UI中slot的用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

