您好,登錄后才能下訂單哦!
這篇文章主要講解了“vue怎么實(shí)現(xiàn)左側(cè)菜單樹形圖遞歸”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“vue怎么實(shí)現(xiàn)左側(cè)菜單樹形圖遞歸”吧!
先說說遇到的坑,由于是子父組件,當(dāng)時(shí)傳遞使用的是子父組件的傳遞,但是這時(shí)候只能獲取到第一層的數(shù)據(jù),第二層往后獲取不到數(shù)據(jù),踩了一下午坑以后才知道,子組件使用了遞歸組件,這時(shí)候他已經(jīng)不能往父組件傳遞了,子傳父,只能逐層傳遞這時(shí)候已經(jīng)隔層了,所以我使用了非子父組件傳遞,兩個(gè)頁面都引入bus.js,這里不懂的請百度。
bus.js
import Vue from 'vue' export default new Vue
父組件內(nèi)容
<ul v-for="menuItem in theModel"> <my-tree :model="menuItem"></my-tree> </ul>
模擬后臺數(shù)據(jù)
theModel:[{ 'id': '1', 'menuName': '導(dǎo)航1', 'menuCode': '10', 'menuUrl':'', 'childMenus': [ { 'menuName': '用戶管理', 'menuCode': '11', 'menuUrl':'/home', 'menuPath':'', 'childMenus':[{ 'menuName': '11111', 'menuCode': '12', 'menuUrl':'/systemjuri', 'menuPath':'', 'childMenus': [] }] }, { 'menuName': '角色管理', 'menuCode': '12', 'menuUrl':'/systemjuri', 'menuPath':'', 'childMenus': [] }, { 'menuName': '菜單管理', 'menuUrl':'/systemmenu', 'menuCode': '13', 'menuPath':'http://10.63.195.214:8080/menuManage/html/index_3.html', 'childMenus':[] }] },{ 'id': '1', 'menuName': '導(dǎo)航2', 'menuCode': '10', 'childMenus':[] }],
父組件引入子組件
import myTree from './treeMenu.vue' export default { components: { myTree }, }
父組件接受子組件傳遞的數(shù)據(jù)
bus.$on("childEvent", function(transmit) {})
下面是子組件內(nèi)容,子組件名稱treeMenu,name: 'treeMenu',
<template> <li> <span @click="toggle(model.menuName,model.menuUrl,model.menuPath)"> <i v-if="!isFolder" class="icon file-text">●</i> {{ model.menuName }} <i v-if="isFolder" class="icon" :class="[open ? 'folder-open': 'folder']"></i> </span> <ul v-show="open" v-if="isFolder"> <tree-menu v-for="item in model.childMenus" :model="item" :key="item.menuId"></tree-menu> </ul> </li> </template> <script> import bus from "./../../../static/js/bus"; export default { name: 'treeMenu', props: ['model'], data() { return { open: false, } }, computed: { isFolder() { return this.model.childMenus && this.model.childMenus.length } }, methods: { toggle(msg,menuUrl,menuPath) { if (this.isFolder) { this.open = !this.open } var json = { msg: msg, menuUrl: menuUrl,menuPath:menuPath }; bus.$emit("childEvent", json) }, } } </script> <style> ul { list-style: none; } i.icon { display: inline-block; width: 15px; height: 15px; background-repeat: no-repeat; vertical-align: middle; float: right; margin-top: 17px; margin-right:30px; } .icon.folder { background-image: url('./homeimg/left_1.png'); } .icon.folder-open { background-image: url('./homeimg/dowm_1.png'); } .icon.file-text { } ul li ul li .icon.folder { background-image: url('./homeimg/left_2.png'); } ul li ul li .icon.folder-open { background-image: url('./homeimg/down_2.png'); } .tree-menu li { line-height: 50px; } span{ display: block; width: 100%; height: 100%; } ul{ padding-left:10px; } ul li span{ text-indent: 10px; } ul li ul{ background:#ebf1f8; color:#1457a7; } li:hover{ cursor:pointer; } </style>
由于用了遞歸組件所以name需要和<tree-menu>
對應(yīng)起來。
Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫只關(guān)注視圖層,方便與第三方庫和項(xiàng)目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫開發(fā)復(fù)雜的單頁應(yīng)用。
感謝各位的閱讀,以上就是“vue怎么實(shí)現(xiàn)左側(cè)菜單樹形圖遞歸”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對vue怎么實(shí)現(xiàn)左側(cè)菜單樹形圖遞歸這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。