<div class="sidebar">
<el-menu
class="sidebar-el-menu"
:default-active="onRoutes"
:collapse="collapse"
background-color="#324157"
text-color="#bfcbd9"
active-text-color="#20a0ff"
unique-opened
router
>
<template v-for="item in items" :v-if="item.isShow===1">
<template v-if="item.subs">
<el-submenu :index="item.index" :key="item.index">
<template slot="title">
<i :class="item.icon"></i>
<!-- <i class="iconfont icon-renwu"></i> -->
<span slot="title">{{ item.title }}</span>
</template>
<template v-for="subItem in item.subs">
<!--<el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">-->
<!--<template slot="title">{{ subItem.title }}</template>-->
<!--<el-menu-item v-for="(threeItem,i) in subItem.subs" :key="i" :index="threeItem.index">-->
<!--{{ threeItem.title }}-->
<!--</el-menu-item>-->
<!--</el-submenu>-->
<el-menu-item
v-if="subItem.isShow===1"
:index="subItem.index"
:key="subItem.index"
><i class="iconfont icon-admin_dian"></i>{{ subItem.title }}</el-menu-item>
</template>
</el-submenu>
</template>
<template v-else>
<el-menu-item :index="item.index" :key="item.index">
<i :class="item.icon"></i>
<span slot="title">{{ item.title }}</span>
</el-menu-item>
</template>
</template>
</el-menu>
</div>
JavaScript代碼
beforeCreate: function() { //這里出現了生命周期鈎子函數,如果是created就無效 //對於created和mounted的區別,總結就是created的dom還沒被vue的dom替換,其他都是一樣的 let self = this; let leftMenu = [ ]; this.postRequest("admin/leftMenu", { token: "" }).then(function(response) { for (let num of response.data.data) { let menuItem = {}; menuItem.subs = []; menuItem.index = num.menuName; menuItem.title = num.menuNameCn; menuItem.icon = `iconfont icon-${num.menuName}`; menuItem.isShow = num.isShow; if (num.childMenu !== null) { for (let subNum of num.childMenu) { let subMenu = {}; subMenu.index = subNum.menuName; subMenu.isShow = subNum.isShow; subMenu.title = subNum.menuNameCn; //關於對象數組的遍歷獲取,肯定有更便捷的方法…… menuItem.subs.push(subMenu); //這里是轉換數據格式而不是遍歷判斷,模板中判斷了一次有沒有二三級菜單,JS中也需要,來轉換數據格式 } } leftMenu.push(menuItem); } self.items = leftMenu.concat(self.items); }); }, data() { return { collapse: false, // items:[] items: [], }; }, computed: { onRoutes() { return this.$route.path.replace("/", ""); } }, created() { // 通過 Event Bus 進行組件間通信,來折疊側邊欄 bus.$on("collapse", msg => { this.collapse = msg; }); } };
可以同過event bus進行側邊欄的折疊效果
