admin渲染左側欄
最近在寫后台管理系統的 時候左側欄是根據 后台的數據進行生成的,本來對這個框架都不熟悉的我,各種百度搜索,結果沒有我想要的結果。最后還是自己慢慢看代碼解決的,首先是我走進了一個 盲區,總是想去通過路由來改變左側欄,但是路由肯定不能動啊,不然怎么跳轉,后來發現左側欄是 獲取路由列表來渲染的,那我改變這個列表不就行了,既然有了思路就只開始動手吧。
結構目錄

找到紅色框里面的 index.vue文件然后 你進去就會豁然開朗 原來左側欄是這么生成的
<template> <div :class="{'has-logo':showLogo}"> <!-- <logo v-if="showLogo" :collapse="isCollapse" /> --> <el-scrollbar wrap-class="scrollbar-wrapper"> <el-menu :default-active="activeMenu" :collapse="isCollapse" :background-color="variables.menuBg" :text-color="variables.menuText" :unique-opened="$store.state.settings.uniqueOpened" :active-text-color="variables.menuActiveText" :collapse-transition="false" mode="vertical" >
<!--改成aaa可以測試靜態效果--> <sidebar-item v-for="route in all" :key="route.path" :item="route" :base-path="route.path" /> </el-menu> </el-scrollbar> </div> </template> <script> import { mapGetters ,mapState,mapActions} from 'vuex' import Logo from './Logo' import SidebarItem from './SidebarItem' import variables from '@/assets/styles/variables.scss' export default { components: { SidebarItem, Logo }, data(){ return{ aaa:[//這個aaa是我測試的時候寫的一個數組就是看看能不能改變 左側欄答案是可以的 首先 path是必須的 路徑你和你們的后台商量怎么寫,
//但是必須匹配你的組件名 meta必須的這是展示在左側的標題 如果有下級就加上children {// 我的工作 path: '/myworker', meta: { title: '我的工作', icon: 'work' }, children: [ { path: 'index', meta: { title: '待辦任務', icon: '00', noCache: true } }, { path: 'readytask', meta: { title: '已辦任務', icon: '00', noCache: true } }, { path: 'endtask', meta: { title: '辦結任務', icon: '00', noCache: true } } ] } ], outList:JSON.parse(localStorage.getItem('sidebarList')).secondMenu, innerList:JSON.parse(JSON.parse(localStorage.getItem('sidebarList')).thirdMenu), all:[] } }, mounted(){ this.endList(this.outList,this.innerList) }, methods:{ endList(parents,childrens){ //因為后台返回了兩個數組一個是 父級 一個是子級 然后根據pid 生成樹形結構封裝的方法 let translator = (parents, children) => { //遍歷父節點數據 parents.unshift({ path: 'dashboard', name: 'myhome', meta: { title: '首頁', icon: 'home', breadcrumb: true }, hidden: false, // 在側邊欄上顯示 true為不顯示 當父路由的字路由為1個時,不顯示父路由,直接顯示子路由 alwaysShow: false// 默認是false 設置為true時會忽略設置的權限 一致顯示在跟路由上 }) parents.forEach((parent,index) => { if(index!=0){ console.log(8811,parent) parent['meta'] = { title: parent.sm_name, icon: parent.sm_cssstyle } parent['path'] = parent.sm_url } //遍歷子節點數據 childrens.forEach((current, index) => { //此時找到父節點對應的一個子節點 if (current.pid === parent.sm_id) { //對子節點數據進行深復制,這里只支持部分類型的數據深復制,對深復制不了解的童靴可以先去了解下深復制 current['meta'] = { title: current.text, icon: '00', noCache: true } current['path'] = current.attributes.url let temp = JSON.parse(JSON.stringify(childrens)) //讓當前子節點從temp中移除,temp作為新的子節點數據,這里是為了讓遞歸時,子節點的遍歷次數更少,如果父子關系的層級越多,越有利 temp.splice(index, 1) //讓當前子節點作為唯一的父節點,去遞歸查找其對應的子節點 // translator([current], temp) //把找到子節點放入父節點的childrens屬性中 typeof parent['children'] !== 'undefined' ? parent['children'].push(current) : parent['children'] = [current] } }) }) } translator(parents,childrens) this.all = parents } }, computed: { ...mapGetters([ 'permission_routers', 'sidebar' ]), activeMenu() { const route = this.$route const { meta, path } = route // if set path, the sidebar will highlight the path you set if (meta.activeMenu) { return meta.activeMenu } return path }, showLogo() { return this.$store.state.settings.sidebarLogo }, variables() { return variables }, isCollapse() { return !this.sidebar.opened } } } </script>
