Vue项目菜单导航封装(一级菜单在上面,二级三级菜单在左侧)

效果图:

《Vue项目菜单导航封装(一级菜单在上面,二级三级菜单在左侧)》

 

第一步,封装上方水平一级菜单

<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 style="cursor: pointer; font-size: 16px" @click="contact">联系客服</div>

                    <!-- 用户头像 -->
                    <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: 'hunter', title: '测试1' },
                { index: 'job', title: '测试2' },
                { index: 'system', 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');
            }
        },
        // 联系客服
        contact() {
            this.$router.push('/contact');
        }
    },
    mounted() {}
};
</script>

<style scoped>
.wrapper {
    width: 100%;
    height: 100%;
    background: #f0f0f0;
}
.header {
    /* position: relative; */
    position: fixed;
    z-index: 999;
    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>

第二步,封装左侧二级,三级菜单导航

<template>
    <!-- 二级、三级菜单封装 -->
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="toIndex()"
            s
            background-color="white"
            text-color="#7a8297"
            active-text-color="#2d8cf0"
            router
        >
            <div>
                <el-submenu :index="index + ''" v-for="(list, index) in lists" :key="list.id">
                    <template slot="title">
                        <i class="el-icon-location"></i>
                        <span>{
  { list.title }}</span>
                    </template>
                    <template v-for="item in list.children">
                        <el-menu-item :index="item.index" :key="item.id" v-if="item">
                            <span slot="title">{
  { item.title }}</span>
                        </el-menu-item>
                    </template>
                </el-submenu>
            </div>
        </el-menu>
    </div>
</template>

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

<style scoped>
/* 左侧菜单栏定位和位置大小设定 */
.sidebar {
    display: block;
    /* position: absolute; */
    position: fixed;
    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>

第三步,配置路由

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

Vue.use(Router);

export default new Router({
    routes: [{
        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: '/i18n',
            component: () => import('../components/common/I18n.vue'),
            meta: {
                title: '国际化'
            }
        },

        {
            // 系统设置
            path: '/system',
            component: () => import('../page/system/index.vue'),
            meta: {
                title: '系统设置'
            },
            redirect: '/system/menuManage',
            children: [
                {
                    path: 'menuManage',
                    component: () => import('../page/system/menuManage.vue'),
                    meta: {
                        title: '菜单管理'
                    }
                },
                {
                    path: 'contentManage',
                    component: () => import('../page/system/contentManage.vue'),
                    meta: {
                        title: '内容管理'
                    }
                },
                {
                    path: 'roleManage',
                    component: () => import('../page/system/roleManage.vue'),
                    meta: {
                        title: '角色管理'
                    }
                },
                {
                    path: 'systemManage',
                    component: () => import('../page/system/systemManage.vue'),
                    meta: {
                        title: '系统设置'
                    }
                },
            ]
        },
        {
            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'
    }
    ]
});

第四步,在页面中应用

<template>
    <div class="system">
        <!-- 一级菜单下面所拥有的二级菜单 -->
        <el-aside>
            <SideMenu :lists="lists"></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 {
            lists: [
                {
                    title: '菜单管理',
                    children: [
                        { index: 'menuManage', title: '菜单管理' },
                        { index: 'contentManage', title: '内容管理' },
                        { index: 'roleManage', title: '角色管理' }
                    ]
                },
                {
                    title: '系统管理',
                    children: [
                        {
                            index: 'systemManage',
                            title: '系统设置'
                        }
                    ]
                }
            ]
        };
    }
};
</script>

<style scoped>
</style>

    原文作者:Redundantº
    原文地址: https://blog.csdn.net/qq_45256497/article/details/123733400
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞