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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 中标麒麟安装deb命令_麒麟源码

    中标麒麟安装deb命令_麒麟源码**中标麒麟NeoKylin-SDK里都有哪些库文件**下边是中标麒麟1-8和14的安装包内容。希望对中标麒麟开发的同学能有些帮助。[root@bogonNeoKylin-SDK]#shinstall.shPleaseselectwhichgroupyouwanttoinstall:1)C-development5)gnome-soft…

    2022年8月10日
    92
  • 第五章 模拟跳转充值界面以及requestCode和resultCode的解释 2.8

    第五章 模拟跳转充值界面以及requestCode和resultCode的解释 2.81.requestCode和resultCoderequestCode:由跳转前的界面定义,通过startActivityForResult()来将定义的intent(同时也代表着要跳转的界面)与

    2022年7月3日
    26
  • 个人服务器搭建违法_自建服务器

    个人服务器搭建违法_自建服务器在win10系统上,安装git,作为客户端安装:Git-2.18.0-64-bit.exe安装步骤:都是简单的安装过程,只截图简单表示下只有第4、8两步有点更改1选择安装路径。234我选择了用notepad++作为编辑器5678修改默认的控制台,用windows的cmd.exe9最后一步创建了10完成转载于:https://…

    2022年9月28日
    0
  • CentOS安装MySQL8.0「建议收藏」

    CentOS安装MySQL8.0「建议收藏」目录配置yum源wget下载源安装包安装源安装MySQL启动相关启动服务查看启动状态设置开机自启重启服务配置修改密码卸载卸载MySQL删除文件配置yum源wget下载源安装包如果没有安装wget,先安装yum-yinstallwget进入mysql官网下载:https://dev.mysql.com/downloads/repo/yum/复制下载链接:https://dev.mysql.com/get/mysql80-community-release-el7-4.noarch.rp

    2022年6月26日
    23
  • Loadrunner教程–常用操做流程

    Loadrunner教程–常用操做流程Loadrunner教程–常用操做流程作者博客:vps精选网1loadrunner压力测试一般使用流程1.1loadrunner压力测试原理本质就是在loadrunner上模拟多个用户同时按固定行为访问web站点。其中固定行为在loadrunner中是通过录制脚本定义的,多个用户同时访问的策略是在loadrunner的场景中定义的。1.2压测思路…

    2022年5月23日
    69
  • 国产自动化测试工具「建议收藏」

    国产自动化测试工具「建议收藏」软件测试工具一直是国外的占据了主流!国内鲜有自主研发的。但是最近几年国内逐渐出现了几个,也许发展下去可以与国外主流软件测试工具厂商一较高下。 AutoRunnerhttp://www.spasvo.com/泽众软件TAR终端自动化测试工具宣称是国内最好的银行业务测试工具。 Tester360http://www.tester360.com/亚

    2022年6月5日
    52

发表回复

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

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