后台管理系统实现多级菜单路由

在Vue.js中创建后台管理系统时,通常会设计一个包含左侧菜单和右侧主要内容的固定布局。本文介绍了如何实现这种布局,特别是如何处理多级菜单路由。通过设置hidden属性控制菜单是否显示,以及alwaysShow属性确保在只有一个子路由的情况下仍能显示父级菜单。

在vue实现后台管理系统时一般都有一个固定的界面布局,往往都会有左侧sider为菜单路由,右侧分header信息和main内容的展示。下面是一种比较常见的后台系统布局。

结构:
在这里插入图片描述

1、layout第一层路由视图

<template>
  <el-container class="app-container">
    <el-aside class="app-aside" :width="isCollapse?'60px':'200px'">
      <router-link to="/" class="aside-title">
        <img src="@/assets/logo.png" alt="">
        <span v-if="!isCollapse" style="padding-left:10px;">项目名称</span>
      </router-link>
      <side-bar :isCollapse="isCollapse"></side-bar>
    </el-aside>
    <el-container style="min-width:1200px;">
      <el-header class="app-header">
        <nav-bar></nav-bar>
      </el-header>
      <el-main class="app-main">
        <router-view/>
      </el-main>
    </el-container>
</el-container>
</template>

<script>
import sideBar from "./components/SideBar"
import navBar from "./components/Navbar"
export default {
  name:"layout",
  components:{
    sideBar,
    navBar
  },
  data(){
    return {
      isCollapse:false
    }
  },
  created(){
    window.addEventListener("resize",this.resizeHandler)
  },
  mounted(){
    this.resizeHandler()
  },
  methods:{
    resizeHandler(){
      if(document.body.getBoundingClientRect().width <1024) {
        this.isCollapse = true
      }else {
        this.isCollapse = false
      }
    }
  }
}
</script>

2、左侧部分的嵌套菜单路由

<template>
  <el-menu
    :router="true"
    :collapse="isCollapse"
    :collapse-transition="true"
    :default-active="$route.path"
    background-color="#304156"
    text-color="#BFCBD9"
    active-text-color="#4087CF">
    <menu-item v-for="route in menuMap" :key="route.path" :item="route" :base-path="route.path"></menu-item>
  </el-menu>
</template>

<script>
import MenuItem from './MenuItem.vue'
export default {
  name:"SideBar",
  components:{
    MenuItem
  },
  props:{
    isCollapse:{
      type:Boolean
    }
  },
  computed:{
    menuMap(){
      return this.$router.options.routes
    }
  }
}
</script>

3、hidden属性可以设置是否隐藏路由,alwaysShow可以设置在只有一个子路由的情况下是否展示父级菜单。

<template>
  <div v-if="!item.hidden && activeChildren.length > 0">
    <!-- 一级 -->
    <el-menu-item v-if="hasOneShowingChild(activeChildren,item) && !item.alwaysShow && (!singleShowChild.child || singleShowChild.isParent)" :index="basePath+'/'+singleShowChild.path">
      <i class="el-icon-menu"></i>
      <span>{{singleShowChild.meta.title}}</span>
    </el-menu-item>
    <!-- 多级 -->
    <el-submenu v-else :index="basePath+'/'+item.path">
      <!-- 一级 -->
      <template slot="title">
        <i class="el-icon-location"></i>
        <span>{{item.meta.title}}</span>
      </template>
      <template v-for="child in activeChildren">
        <!-- 三级 -->
        <menu-item v-if="child.children && child.children.length>0" 
          :item="child" 
          :base-path="child.path+'/'+child.path" 
          v-bind="$attrs" 
          :key="child.name">
        </menu-item>
        <!-- 二级 -->
        <el-menu-item v-else :index="basePath+'/'+child.path" :key="child.name">
          <i class="el-icon-menu"></i>
          <span>{{child.meta.title}}</span>
        </el-menu-item>
      </template>
    </el-submenu>
  </div>
</template>

<script>
export default {
  name:"MenuItem",
  props:{
    item:{
      type:Object,
      required:true
    },
    basePath:{
      type:String,
      default:""
    }
  },
  data(){
    return {
      singleShowChild:null
    }
  },
  computed:{
    activeChildren() {

      if(!this.item.children) return [];
      return this.item.children.filter(child => !child.hidden);
    }
  },
  methods:{
    hasOneShowingChild(children, parent) {

      if(children.length === 1) {
        this.singleShowChild = children[0];
        return true;
      }

      if(children.length === 0) {
        this.singleShowChild = { ...parent, path: '', isParent: true}
        return true;
      }

      return false;
    }
  }
}
</script>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值