一、目的
后台管理系统页面的整体结构如图:
实现顶部一级菜单和左侧二级菜单进行响应,选哪个一级菜单时,左侧菜单栏有相对应下的二级菜单,产生联动效果。然后选不同的二级菜单,主体内容区域展示对应内容。
二、效果图
后台管理系统模板源码的 github 地址:https://github.com/hxhpg/vue-secondMenu-test
三、具体操作实现
1、用 vue-cli 创建的 Vue 项目后,自己再手动搭建项目的基本目录结构,如图:
2、创建一个公共组件 Whole.vue 来实现整体页面结构的布局,通过 div 来进行区域定位和大小设定。
使用 el-menu 组件的 mode 属性为 "horizontal" 来使导航菜单变更为水平模式。
@select="handleSelect" 来切换不同的一级菜单展示不同的二级菜单栏和主体内容区域,用 router-view 通过路由来进行组件页面跳转,代码如下:
<template>
<div class="wrapper">
<!-- 页面头部部分 -->
<div class="header">
<div class="logo">后台管理系统</div>
<!-- 水平一级菜单 -->
<div style="float:left;">
<el-menu
mode="horizontal"
text-color="#000000"
active-text-color="#3989fa"
:default-active="toIndex"
@select="handleSelect">
<el-menu-item v-for="(item, index) in itemList" :index="item.path" :key="index">
<span slot="title">{{ item.title }}</span>
</el-menu-item>
</el-menu>
</div>
<div class="header-right">
<div class="header-user-con">
<!-- 用户头像 -->
<div class="user-avator">
<img src="@/assets/img/img.jpg"/>
</div>
<!-- 用户名下拉菜单 -->
<el-dropdown class="user-name" trigger="click" @command="handleCommand">
<span class="el-dropdown-link"> {{ username }} <i class="el-icon-caret-bottom"></i></span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item disabled>修改密码</el-dropdown-item>
<el-dropdown-item command="loginout">退出登录</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</div>
</div>
</div>
<!-- 页面左侧二级菜单栏,和主体内容区域部分 -->
<el-main>
<router-view></router-view>
</el-main>
</div>
</template>
<script>
import bus from "@/utils/bus";
export default {
data(){
return{
itemList: [ // 水平一级菜单栏的菜单
{ path: '/Home', title: '首页' },
{ path: '/test1', title: '一级菜单1' },
{ path: '/test2', title: '一级菜单2' },
{ path: '/test3', title: '一级菜单3' },
{ path: '/permission', title: '管理员权限' },
// { path: '/i18n', title: '国际化组件' }
],
}
},
computed: {
username(){
return localStorage.getItem('ms_username') || '';
},
toIndex(){ // 根据路径绑定到对应的一级菜单,防止页面刷新重新跳回第一个
return '/' + this.$route.path.split('/')[1];
},
},
created() {
},
methods: {
handleSelect(path){ // 切换菜单栏
this.$router.push({
path: path
});
},
handleCommand(command){ // 用户名下拉菜单选择事件
if(command == 'loginout'){
localStorage.removeItem('ms_username');
this.$router.push({
path: '/Login'
});
}
}
}
}
</script>
<style scoped>
.wrapper {
width: 100%;
height: 100%;
background: #f0f0f0;
}
.header {
position: relative;
box-sizing: border-box;
width: 100%;
height: 70px;
font-size: 22px;
}
.header .logo {
float: left;
margin-left: 60px;
margin-top: 17.5px;
height: 29px;
width: 160px;
vertical-align: middle;
}
/* --------------- 用户头像区域的样式 ---------------- */
.header-right {
float: right;
padding-right: 50px;
}
.header-user-con {
display: flex;
align-items: center;
justify-content: center;
height: 70px;
}
.user-avator {
margin-left: 20px;
}
.user-avator img {
display: block;
width: 40px;
height: 40px;
border-radius: 50%;
}
.user-name {
margin-left: 10px;
}
.el-dropdown-link {
cursor: pointer;
}
.el-dropdown-menu__item {
text-align: center;
}
/* --------------- 水平一级菜单栏的样式--------------------- */
.el-menu.el-menu--horizontal {
border-bottom: none !important;
float: left;
margin-left: 50px;
background: transparent;
}
.el-menu--horizontal > .el-menu-item.is-active {
/* border-bottom: 2px solid #3989fa;
color: #3989fa; */
font-weight: bold;
}
.el-menu--horizontal > .el-menu-item {
font-size: 16px;
margin: 0 15px;
}
</style>
3、这样就把页面顶部的一级菜单栏展现出来了。接下来要封装一个左侧二级菜单栏的组件 SideMenu.vue ,设定 props 是让调用该组件时把传递的值获取到。代码如下:
<template>
<!-- 左侧二级菜单栏的组件封装 -->
<div class="sidebar">
<el-menu
class="sidebar-el-menu"
:default-active="toIndex"
background-color="#fff"
text-color="#000000"
router>
<el-menu-item v-for="(item, index) in itemList" :key="index" :index="item.path">
<!-- 需要图标的在 item 对象中加上属性 icon -->
<!-- <i :class="item.icon"></i> -->
<span slot="title">{{ item.title }}</span>
</el-menu-item>
</el-menu>
</div>
</template>
<script>
export default {
props: {
itemList: {
type: Array,
default: () => []
}
},
data(){
return {
}
},
computed: {
toIndex(){ // 根据路径绑定到对应的二级菜单,防止页面刷新重新跳回第一个
return this.$route.path.split('/')[2];
}
},
methods:{
},
}
</script>
<style lang="scss" scoped>
/* 左侧菜单栏定位和位置大小设定 */
.sidebar {
display: block;
position: absolute;
left: 0;
top: 70px;
bottom: 0;
overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
width: 0;
}
.sidebar-el-menu {
width: 250px;
}
.sidebar > ul {
height: 100%;
}
/* 左侧二级菜单项的样式 */
.el-menu-item{
font-size: 14px !important;
padding-left: 35px !important;
}
/* 左侧二级菜单选中时的样式 */
.el-menu-item.is-active {
color: white !important;
background: #3989fa !important;
}
.el-menu-item, .el-submenu__title {
height: 50px !important;
line-height: 50px !important;
}
</style>
4、然后在各个一级菜单下( 项目目录的 page 文件夹下的各个文件夹里的 index.vue )调用左侧二级菜单栏组件。
每个 index.vue 会对应每个一级菜单,这个通过改变路径(@select="handleSelect")和对路由进行配置来实现跳转,通过组件之间的传值通信把二级菜单项传给左侧二级菜单栏的组件。
再次用 router-view 通过路由来进行组件页面跳转(这样就形成两层嵌套,进行两次 router-view 路由跳转),index.vue 代码如下:
<template>
<div>
<!-- 一级菜单下面所拥有的二级菜单 -->
<el-aside>
<SideMenu :itemList='itemList'></SideMenu>
</el-aside>
<!-- 以及二级菜单所对应的页面 -->
<el-main>
<router-view></router-view>
</el-main>
</div>
</template>
<script>
import SideMenu from '@/components/sidemenu/SideMenu';
export default {
components: {
SideMenu
},
data(){
return{
itemList: [
{ path: 'test1-1', title: '二级菜单1-1' },
{ path: 'test1-2', title: '二级菜单1-2' },
{ path: 'test1-3', title: '二级菜单1-3' },
{ path: 'test1-4', title: '二级菜单1-4' },
{ path: 'test1-5', title: '二级菜单1-5' }
]
}
}
}
</script>
5、一二级菜单联动效果所需的组件和页面基本弄好了,最后得将这些都拼接起来走通,要对 vue-router 进行配置( router 文件夹下 index.js 文件),代码如下:
import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err);
}
export default new VueRouter({
routes: [
{
path: '/', // 程序启动默认路由
component: () => import('@/components/common/Whole.vue'),
meta: { title: '整体页面布局' },
redirect: '/Home', // 重定向到首页
children: [
{
path: '/Home',
component: () => import('@/page/Home.vue'),
meta: { title: '首页' }
},
{
path: '/test1',
component: () => import('@/page/test1/index.vue'),
meta: { title: '一级菜单1' },
redirect: '/test1/test1-1', // 该配置是若点击选择一级菜单时,默认选中并跳转到该一级菜单下的第一个二级菜单
children:[
{
path: 'test1-1',
component: () => import('@/page/test1/test1-1.vue'),
meta: { title: '二级菜单1-1' },
},
{
path: 'test1-2',
component: () => import('@/page/test1/test1-2.vue'),
meta: { title: '二级菜单1-2' },
},
{
path: 'test1-3',
component: () => import('@/page/test1/test1-3.vue'),
meta: { title: '二级菜单1-3' },
},
{
path: 'test1-4',
component: () => import('@/page/test1/test1-4.vue'),
meta: { title: '二级菜单1-4' },
},
{
path: 'test1-5',
component: () => import('@/page/test1/test1-5.vue'),
meta: { title: '二级菜单1-5' },
}
]
},
{
path: '/test2',
component: () => import('@/page/test2/index.vue'),
meta: { title: '一级菜单2' },
redirect: '/test2/test2-1', // 该配置是若点击选择父目录时,默认选中该父目录下的子路径页面
children:[
{
path: 'test2-1',
component: () => import('@/page/test2/test2-1.vue'),
meta: { title: '二级菜单2-1' },
},
{
path: 'test2-2',
component: () => import('@/page/test2/test2-2.vue'),
meta: { title: '二级菜单2-2' },
},
{
path: 'test2-3',
component: () => import('@/page/test2/test2-3.vue'),
meta: { title: '二级菜单2-3' },
},
]
},
{
path: '/test3',
component: () => import('@/page/test3/index.vue'),
meta: { title: '一级菜单3' },
redirect: '/test3/test3-1',
children:[
{
path: 'test3-1',
component: () => import('@/page/test3/test3-1.vue'),
meta: { title: '二级菜单3-1' }
},
{
path: 'test3-2',
component: () => import('@/page/test3/test3-2.vue'),
meta: { title: '二级菜单3-2' }
},
]
},
{
path: '/i18n', // 国际化组件
component: () => import('@/components/common/I18n.vue'),
meta: { title: '国际化' }
},
{
path: '/permission', // 权限页面
component: () => import('@/page/Permission.vue'),
meta: {
title: '权限测试',
permission: true
}
},
{
path: '/404',
component: () => import('@/page/404.vue'),
meta: { title: '404' }
},
{
path: '/403',
component: () => import('@/page/403.vue'),
meta: { title: '403' }
},
]
},
{
path: '/Login', // 登录页面
component: () => import('@/page/Login.vue'),
meta: { title: '登录' }
},
{
path: '*',
redirect: '/404'
}
]
});
6、顶部一级菜单栏,左侧二级菜单栏的功能效果完成了,接下来就可以去开发具体的主体内容区域的页面了。例如 test1-2.vue 的代码如下:
<template>
<div class="content-box">
<div class="container">
<p>主体页面 1 - 2 </p>
<div class="test-div">
<i class="el-icon-edit"></i>
<i class="el-icon-share"></i>
<i class="el-icon-delete"></i>
</div>
</div>
</div>
</template>
<script>
export default {
data(){
return{
}
}
}
</script>
<style>
.test-div i{
font-size: 25px;
}
</style>
其中主体内容区域的定位和位置大小设定的 css 如下( 这些样式是公共,可复用的,单独写一个样式文件来全局引入使用 ):
/* 页面主体部分 */
.content-box {
position: absolute;
left: 250px; /* 控制左侧二级菜单栏的宽度 */
right: 0;
top: 70px;
bottom: 0;
padding: 10px 20px;
-webkit-transition: left .3s ease-in-out;
transition: left .3s ease-in-out;
background: #f0f0f0;
}
.content {
width: auto;
height: 100%;
padding: 10px;
overflow-y: scroll;
box-sizing: border-box;
}
.content-collapse {
left: 65px;
}
.container {
padding: 20px; /* 控制主体部分与主体边框的距离 */
background: #fff;
border: 1px solid #ddd;
border-radius: 5px;
}
/* 表格栏样式 */
.table-container {
margin-top: 20px;
}
/* 分页栏样式 */
.pagination-area {
width: 100%;
text-align: right;
margin: 20px 0 10px 0;
}
/* 测试主体页面的 div 样式 */
.test-div{
margin: 15px;
}
整个后台管理系统模板的核心内容开发大致是这样的,更多具体细节可以下载完整的源码来看。
项目实现了登录功能,一二级菜单联动选择展示不同的主体内容部分,封装了 axios 请求库,还简单配置了管理员和普通用户的权限区分。
这是我本人在工作学习中遇到的问题,并对解决问题的方案进行了一次记录,跟小伙伴们分享出来哈 ~ 供参考学习,有什么建议也欢迎评论留言,转载请注明出处哈,感谢支持!
后续扩展文章:
Vue + ElementUI 后台管理系统实现主题风格切换_无解的菜鸟晖的博客-CSDN博客
Vue + ElementUI 后台管理系统实现顶部一级菜单栏,左侧多级菜单栏(二级、三级等)_无解的菜鸟晖的博客-CSDN博客