Vue.js組件tree實現(xiàn)無限級樹形菜單
分享一段用 <ul>和<li>標簽實現(xiàn)tree的代碼,可能寫的不是很好,如果大家有更好的希望分享下。
代碼看這里嘍:
html代碼:
<div class="tree"> <nav class='navbar'> <ul class='nav nav-stacked'> <template v-for='item in menus'> <li role='presentation' v-if='!item.children'><a href="#">{{item.text}}</a></li> <li role='presentation' v-if='item.children'><a href="#" v-on:click='toggleChildren(item)'>{{item.text}}<span class='glyphicon' v-bind:class='{ "glyphicon-chevron-right": !item.expanded, "glyphicon-chevron-down": item.expanded }'></span></a> <ul v-show='item.expanded' class="childs"> <li v-for='child in item.children'><a href="#">{{child.text}}</a></li> </ul> </li> </template> </ul> </nav> </div>
js代碼:
methods: { toggleChildren: function(item) { item.expanded = !item.expanded; }, }, data() { return { menus:[{ text:'水果', expanded:false, children:[{ text:'蘋果', },{ text:'荔枝' },{ text:'葡萄' },{ text:'火龍果' }] },{ text:'好吃的', expanded:false, children:[{ text:'糖', },{ text:'面包' },{ text:'火腿' },{ text:'薯片' },{ text:'碎碎面' }] },{ text:'飲料', expanded:false, children:[] }] } },
效果圖:
本文已被整理到了《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學(xué)習(xí)教程進行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一文詳解Vue如何整合Echarts實現(xiàn)可視化界面
ECharts,縮寫來自Enterprise Charts,商業(yè)級數(shù)據(jù)圖表,一個純Javascript的圖表庫,可以流暢的運行在PC和移動設(shè)備上。本文將在Vue中整合Echarts實現(xiàn)可視化界面,感興趣的可以了解一下2022-04-04Vue.js watch監(jiān)視屬性知識點總結(jié)
在本篇文章里小編給大家分享的是關(guān)于Vue.js watch監(jiān)視屬性的相關(guān)知識點內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-11-11vue調(diào)用本地緩存方式(監(jiān)視數(shù)據(jù)變更)
這篇文章主要介紹了vue調(diào)用本地緩存方式(監(jiān)視數(shù)據(jù)變更),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04使用vue框架 Ajax獲取數(shù)據(jù)列表并用BootStrap顯示出來
這篇文章主要介紹了使用vue框架 Ajax獲取數(shù)據(jù)列表并用BootStrap顯示出來,需要的朋友可以參考下2017-04-04