學(xué)習(xí)vue有一段時(shí)間了,最近使用vue做了一套后臺(tái)管理系統(tǒng),左側(cè)菜單需求是這樣的,可以多層,數(shù)據(jù)由后臺(tái)傳遞。也因?yàn)樽约簩?duì)官方文檔的不熟悉使得自己踩了不少坑,今天寫出來和大家一起分享。
效果圖如下所示:

先說說遇到的坑,由于是子父組件,當(dāng)時(shí)傳遞使用的是子父組件的傳遞,但是這時(shí)候只能獲取到第一層的數(shù)據(jù),第二層往后獲取不到數(shù)據(jù),踩了一下午坑以后才知道,子組件使用了遞歸組件,這時(shí)候他已經(jīng)不能往父組件傳遞了,子傳父,只能逐層傳遞這時(shí)候已經(jīng)隔層了,所以我使用了非子父組件傳遞,兩個(gè)頁面都引入bus.js,這里不懂的請(qǐng)百度。
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>
模擬后臺(tái)數(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>對(duì)應(yīng)起來
總結(jié)
以上所述是小編給大家介紹的vue左側(cè)菜單,樹形圖遞歸實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注