vue左側(cè)菜單,樹形圖遞歸實現(xiàn)代碼
學(xué)習(xí)vue有一段時間了,最近使用vue做了一套后臺管理系統(tǒng),左側(cè)菜單需求是這樣的,可以多層,數(shù)據(jù)由后臺傳遞。也因為自己對官方文檔的不熟悉使得自己踩了不少坑,今天寫出來和大家一起分享。
效果圖如下所示:
先說說遇到的坑,由于是子父組件,當(dāng)時傳遞使用的是子父組件的傳遞,但是這時候只能獲取到第一層的數(shù)據(jù),第二層往后獲取不到數(shù)據(jù),踩了一下午坑以后才知道,子組件使用了遞歸組件,這時候他已經(jīng)不能往父組件傳遞了,子傳父,只能逐層傳遞這時候已經(jīng)隔層了,所以我使用了非子父組件傳遞,兩個頁面都引入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)起來
總結(jié)
以上所述是小編給大家介紹的vue左側(cè)菜單,樹形圖遞歸實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue中ref、reactive、toRef、toRefs、$refs的基本用法總結(jié)
這篇文章主要給大家介紹了關(guān)于Vue中ref、reactive、toRef、toRefs、$refs的基本用法,以及他們之家的區(qū)別,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11vue自適應(yīng)布局postcss-px2rem詳解
這篇文章主要介紹了vue自適應(yīng)布局(postcss-px2rem)的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-05-05Vue引入并使用Element組件庫的兩種方式小結(jié)
本文主要介紹了Vue引入并使用Element組件庫的兩種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01vue項目中data數(shù)據(jù)之間互相訪問的實現(xiàn)
本文主要介紹了vue項目中data數(shù)據(jù)之間互相訪問的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05elementUI同一頁面展示多個Dialog的實現(xiàn)
這篇文章主要介紹了elementUI同一頁面展示多個Dialog的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11vue項目中請求數(shù)據(jù)特別多導(dǎo)致頁面卡死的解決
這篇文章主要介紹了vue項目中請求數(shù)據(jù)特別多導(dǎo)致頁面卡死的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09