本文小編為大家詳細(xì)介紹“vue中怎么實(shí)現(xiàn)左側(cè)菜單和樹形圖遞歸”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“vue中怎么實(shí)現(xiàn)左側(cè)菜單和樹形圖遞歸”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
創(chuàng)新互聯(lián)公司專注于高淳企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),成都做商城網(wǎng)站。高淳網(wǎng)站建設(shè)公司,為高淳等地區(qū)提供建站服務(wù)。全流程按需設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
效果圖如下所示:
先說說遇到的坑,由于是子父組件,當(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>
模擬后臺(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>
對應(yīng)起來。
讀到這里,這篇“vue中怎么實(shí)現(xiàn)左側(cè)菜單和樹形圖遞歸”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:vue中怎么實(shí)現(xiàn)左側(cè)菜單和樹形圖遞歸
轉(zhuǎn)載來源:http://www.rwnh.cn/article10/jeeddo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、App開發(fā)、網(wǎng)站排名、動(dòng)態(tài)網(wǎng)站、網(wǎng)站策劃、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)