Vue + ElementUI 后台管理系统实现顶部一级菜单栏,左侧二级菜单栏

Vue + ElementUI 后台管理系统实现顶部一级菜单栏,左侧二级菜单栏一、要求后台管理系统页面的整体结构如图:实现顶部一级菜单和左侧二级菜单进行响应,选哪个一级菜单时,左侧菜单栏有相对应下的二级菜单,产生联动效果。然后选不同的二级菜单,主体内容区域展示对应内容。二、效果图三、具体操作实现1、用vue-cli创建的Vue项目后,自己再手动搭建项目的基本目录结构,如图:2、创建一个公共组件Whole.vue来实现整体页面结构的布局,通过div来进行区域定位和大小设定。使用el-menu组件的mode…

大家好,又见面了,我是你们的朋友全栈君。

一、目的

后台管理系统页面的整体结构如图:

Vue + ElementUI 后台管理系统实现顶部一级菜单栏,左侧二级菜单栏

实现顶部一级菜单和左侧二级菜单进行响应,选哪个一级菜单时,左侧菜单栏有相对应下的二级菜单,产生联动效果。然后选不同的二级菜单,主体内容区域展示对应内容。

二、效果图

Vue + ElementUI 后台管理系统实现顶部一级菜单栏,左侧二级菜单栏

后台管理系统模板源码的 github 地址:https://github.com/hxhpg/vue-secondMenu-test

三、具体操作实现

1、用 vue-cli 创建的 Vue 项目后,自己再手动搭建项目的基本目录结构,如图:

Vue + ElementUI 后台管理系统实现顶部一级菜单栏,左侧二级菜单栏

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 :default-active="toIndex()" mode="horizontal" @select="handleSelect">
            <template v-for="item in items">
                <el-menu-item :index="item.index" :key="item.index">
                <template slot="title">
                    <span slot="title">{
  
  { item.title }}</span>
                </template>
                </el-menu-item>
            </template>
            </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>
export default {
    data() {
        return {
            items: [    // 水平一级菜单栏的菜单
                { index: 'Home', title: '首页' },
                { index: 'test1', title: '一级菜单1' },
                { index: 'test2', title: '一级菜单2' },
                { index: 'test3', title: '一级菜单3' },
                { index: 'permission', title: '管理员权限' },
            ]
        }
    },
    computed: {
        username() {
            let username = localStorage.getItem('ms_username');
            return username ? username : this.name;
        }
    },
    methods:{
        // 根据路径绑定到对应的一级菜单,防止页面刷新重新跳回第一个
        toIndex() {
            return this.$route.path.split('/')[1];
        },
        // 切换菜单栏
        handleSelect(index) {
            this.$router.push('/' + index);
        },
        // 用户名下拉菜单选择事件
        handleCommand(command) {
            if (command == 'loginout') {
                localStorage.removeItem('ms_username');
                this.$router.push('/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;
    height: 70px;
    align-items: center;
}
.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;
}
.el-menu--horizontal > .el-menu-item.is-active {
    border-bottom: 2px solid #409eff;
    color: #3989fa;
    font-weight: 700;
}
.el-menu--horizontal > .el-menu-item {
    font-size: 16px;
    margin: 0 15px;
    color: black;
}
</style>

3、这样就把页面顶部的一级菜单栏展现出来了。接下来要封装一个左侧二级菜单栏的组件 SideMenu.vue ,设定 props 是让调用该组件时把传递的值获取到。代码如下:

<template>
    <div class="sidebar">
    <!-- 左侧二级菜单栏的组件封装 -->
        <el-menu
            class="sidebar-el-menu"
            :default-active="toIndex()"
            background-color="white"
            text-color="#7a8297"
            active-text-color="#2d8cf0"
            router>
            <template v-for="item in items">
                <el-menu-item :index="item.index" :key="item.index">
                    <!-- 需要图标的在 item 对象中加上属性 icon -->
                    <!-- <i :class="item.icon"></i> -->
                    <span slot="title">{
  
  { item.title }}</span>
                </el-menu-item>
            </template>
        </el-menu>
    </div>
</template>

<script>
export default {
    props: ['items'],
    data() {
        return {

        }
    },
    methods:{
        // 根据路径绑定到对应的二级菜单,防止页面刷新重新跳回第一个
        toIndex(){
            return this.$route.path.split('/')[2];
        },
    },
};
</script>

<style 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;
    color: black !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 :items='items'></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 {
            items: [
            {
                index: 'test1-1',
                title: '二级菜单1-1'
            },
            {
                index: 'test1-2',
                title: '二级菜单1-2'
            },
            {
                index: 'test1-3',
                title: '二级菜单1-3'
            },
            {
                index: 'test1-4',
                title: '二级菜单1-4'
            },
            {
                index: 'test1-5',
                title: '二级菜单1-5'
            }
        ],
        }
    }
}
</script>

<style scoped>
</style>

5、一二级菜单联动效果所需的组件和页面基本弄好了,最后得将这些都拼接起来走通,要对 vue-router 进行配置( router 文件夹下 index.js 文件),代码如下:

import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

export default new Router({
    routes: [{
            path: '/',
            redirect: '/dashboard'
        },
        {
            path: '/',
            component: () => import('../components/common/Whole.vue'),
            meta: {
                title: '整体页面布局'
            },
            children: [{
                    path: '/dashboard',
                    component: () => import('../page/Dashboard.vue'),
                    meta: {
                        title: '首页'
                    },
                    redirect:'/Home',     // 该配置是若点击选择父目录时,默认选中该父目录下的子路径页面
                        children: [{
                            path: '/Home',
                            component: () => import('../page/Dashboard.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: '/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;
}
.container {
    padding: 20px;    /* 控制主体部分与主体边框的距离 */
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 5px;
}
/* 测试主体页面的 div 样式 */
.test-div{
    margin: 15px;
}

整个后台管理系统模板的核心内容开发大致是这样的,更多具体细节可以下载完整的源码来看。

项目实现了登录功能,一二级菜单联动选择展示不同的主体内容部分,封装了 axios 请求库,还配置了管理员和普通用户的权限区分。

这是我本人在工作学习中遇到的问题,并对解决问题的方案进行了一次记录,跟小伙伴们分享出来哈 ~ 供参考学习,有什么建议也欢迎评论留言,转载请注明出处哈,感谢支持!


后续扩展文章:

Vue + ElementUI 后台管理系统实现主题风格切换:https://blog.csdn.net/weixin_41856395/article/details/111173730

Vue + ElementUI 后台管理系统实现顶部一级菜单栏,左侧多级菜单栏(二级、三级等):https://blog.csdn.net/weixin_41856395/article/details/116062604

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134557.html原文链接:https://javaforall.net

(1)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 心脏出血(Heartbleed)漏洞浅析、复现

    心脏出血(Heartbleed)漏洞浅析、复现一、漏洞介绍心脏出血(英语:Heartbleed),也简称为心血漏洞,是一个出现在加密程序库OpenSSL的安全漏洞,该程序库广泛用于实现互联网的传输层安全(TLS)协议。它于2012年被引入了软件中,2014年4月首次向公众披露。只要使用的是存在缺陷的OpenSSL实例,无论是服务器还是客户端,都可能因此而受到攻击。此问题的原因是在实现TLS的心跳扩展时没有对输入进行适当验证(缺少边界检查),因此漏洞的名称来源于“心跳”(heartbeat)。该程序错误属于缓冲区过读,即可以读取的数据比应该允许读取的还

    2022年7月16日
    166
  • spdlog学习笔记

    spdlog学习笔记说明:所有内容翻译自spdlog的wiki,受英语水平所限,有所错误或失真在所难免,如果您有更好的建议,请在博文下留言。线程安全spdlog::命名空间下的是线程安全的,当loggers在不同的线程同时执行时,下述函数不应该被调用:spdlog::set_error_handler(log_err_handler);//orlogger->set_error_handler(…

    2022年6月23日
    22
  • 数值分析(一) 牛顿插值法及matlab代码

    数值分析(一) 牛顿插值法及matlab代码目录数学:数值分析一、牛顿插值法原理1.牛顿插值多项式2.差商2.1定义2.2性质2.3差商表3.牛顿(Newton)插值公式二、牛顿插值公式matlab代码1.matlab实时在线脚本2.牛顿插值代码3.实例三、总结数学:数值分析  刚上完数值分析课在其中学习了不少的知识,课后还做了一些课程实验主要都是利用matlab编程来解决问题,接下先讲插值法中的牛顿插值法一、牛顿插值法原理1.牛顿插值多项式  定义牛顿插值多项式为:Nn(x)=a0+a1(x−x0)+a2(x−x0)(x−

    2022年6月3日
    32
  • 探讨PMI测量配置对5G下行速率的影响

    探讨PMI测量配置对5G下行速率的影响问题描述 当前 5GPMI 测量配置均为 8P4B 那么什么是 8P4B 另外为什么功能开启后能提升下行速率 问题分析 首先简单回答 8P4B 是一组下行参考信号 CSI RS UE 测量 CSI RS 信号用于给基站反馈下行 CQI PMI RI CRI 等信息 当基站读到信息后会给 UE 分配最合适的 MCS TBSize RB 资源等调度相关的参数 从而使 UE 的下载性能达到最佳 一 8PP 是 port 的缩写 就是 8 个端口 这里的端口是逻辑天线端口 下图是按照目前中兴推荐配置后的一个 RB 内的 8 个端口

    2025年9月4日
    4
  • mac phpstrom 激活码【2022最新】「建议收藏」

    (mac phpstrom 激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html4KDDGND3CI-eyJsa…

    2022年4月1日
    107
  • pycharm将代码同步到远程服务器_pycharm连接python调试器失败

    pycharm将代码同步到远程服务器_pycharm连接python调试器失败pycharm远程调试程序时出现“Couldn’tconnecttoconsoleprocess.Processfinishedwithexitcode-1”针对于错误代码为-1的情况,本人解决方式如下:pycharm→\rightarrow→EditConfigrations→\rightarrow→python→\rightarrow→Runwithp…

    2022年8月27日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号