之前写过一个类似的效果,vue element ui NavMenu 导航菜单 自定义样式,这次需要一个白色背景的,有部分逻辑变动,记录一下,方便后期选择使用。
使用
<main-sidebar :openeds="[1]" active="apiManagement"/>
组件代码
/**
* @Author: 858834013@qq.com
* @Name: sidebar
* @Date: 2022-07-22
* @Desc: 导航自定义样式
*/
<template>
<div class="sidebar">
<div class="logo">
<img src="../assets/nav/logo.png" alt="">
</div>
<div class="sidebarList hideScrollBar">
<div class="menus">
<el-menu
default-active="2"
class="el-menu-vertical-demo"
@open="handleOpen"
:unique-opened="true"
@select="handleSelect"
:default-openeds="openeds"
:default-active="active"
@close="handleClose">
<el-submenu :index="index" :class="{active:!item.children}" v-for="(item,index) in list" :key="index">
<template slot="title">
<div class="title" @click="goUrlHome(item)">
<div class="icon">
<i class="icons" :class="item.icon"></i>
</div>
<span>{{ item.name }}</span>
</div>
</template>
<el-menu-item-group>
<div @click="goUrl(item2)" v-for="(item2,index2) in item.children" :key="index2">
<el-menu-item :index="item2.index">{{ item2.name }}
<div class="line"></div>
</el-menu-item>
</div>
</el-menu-item-group>
</el-submenu>
</el-menu>
</div>
</div>
</div>
</template>
<script>
export default {
name: "sidebar",
components: {},
props: {
openeds: {
type: Array,
default() {
return [0];
}
},
active: {
type: String,
default() {
return ''
}
},
},
data() {
return {
isActive: false,
list: [
{
"id": "1",
"pid": "0",
"name": "首页",
"url": "/home",
"type": 0,
"icon": "icon1",
"permissions": "",
"sort": 0,
"createDate": "2022-07-14 12:04:00",
"parentName": null
},
{
"id": "2",
"pid": "0",
"children": [
{
"id": "21",
"pid": "21",
"children": [],
"name": "API管理",
"url": "/product/apiManagement",
"type": 0,
"icon": "",
"index": 'apiManagement',
"permissions": "",
"sort": 0,
"createDate": "2022-07-14 12:04:00",
"parentName": null
}, {
"id": "21",
"pid": "21",
"children": [],
"name": "客户白名单管理",
"url": "/product/whiteList",
"type": 0,
"icon": "",
"index": 'whiteList',
"permissions": "",
"sort": 0,
"createDate": "2022-07-14 12:04:00",
"parentName": null
},
],
"name": "产品管理",
"url": "",
"type": 0,
"icon": "icon2",
"permissions": "",
"sort": 0,
"createDate": "2022-07-14 12:04:00",
"parentName": null
},
{
"id": "3",
"pid": "0",
"children": [
{
"id": "31",
"pid": "3",
"children": [],
"name": "客户列表",
"url": "sys/customer",
"type": 0,
"index": 'customer',
"icon": "",
"permissions": "",
"sort": 0,
"createDate": "2022-07-14 12:04:00",
"parentName": null
}
],
"name": "客户管理",
"url": "",
"type": 0,
"icon": "icon3",
"permissions": "",
"sort": 0,
"createDate": "2022-07-14 12:04:00",
"parentName": null
},
{
"id": "4",
"pid": "0",
"children": [
{
"id": "41",
"pid": "4",
"children": [],
"name": "发票管理",
"url": "financial/invoice",
"type": 0,
"icon": "",
"index": 'invoice',
"permissions": "",
"sort": 2,
"createDate": "2022-07-14 12:04:00",
"parentName": null
}
],
"name": "财务管理",
"url": "/",
"type": 0,
"icon": "icon4",
"permissions": "",
"sort": 0,
"createDate": "2022-07-14 12:04:00",
"parentName": null
},
{
"id": "5",
"pid": "0",
"children": [
{
"id": "51",
"pid": "5",
"children": [],
"name": "API订单",
"url": "sys/apiOrders",
"type": 0,
"icon": "",
"index": 'apiOrders',
"permissions": "",
"sort": 2,
"createDate": "2022-07-14 12:04:00",
"parentName": null
}
],
"name": "订单管理",
"url": "",
"type": 0,
"icon": "icon5",
"permissions": "",
"sort": 0,
"createDate": "2022-07-14 12:04:00",
"parentName": null
},
{
"id": "6",
"pid": "0",
"children": [
{
"id": "61",
"pid": "6",
"children": [],
"name": "人员管理",
"url": "sys/personnelManagement",
"type": 0,
"icon": "",
"index": 'personnelManagement',
"permissions": "",
"sort": 2,
"createDate": "2022-07-14 12:04:00",
"parentName": null
}, {
"id": "62",
"pid": "6",
"children": [],
"name": "角色管理",
"url": "sys/roleManagement",
"type": 0,
"index": 'roleManagement',
"icon": "",
"permissions": "",
"sort": 2,
"createDate": "2022-07-14 12:04:00",
"parentName": null
},
],
"name": "系统管理",
"url": "",
"type": 0,
"icon": "icon6",
"permissions": "",
"sort": 0,
"createDate": "2022-07-14 12:04:00",
"parentName": null
},
{
"id": "7",
"pid": "0",
"children": [
{
"id": "71",
"pid": "7",
"children": [],
"name": "轮播列表",
"url": "sys/banner",
"index": 'banner',
"type": 0,
"icon": "",
"permissions": "",
"sort": 2,
"createDate": "2022-07-14 12:04:00",
"parentName": null
}
],
"name": "轮播管理",
"url": "",
"type": 0,
"icon": "icon7",
"permissions": "",
"sort": 0,
"createDate": "2022-07-14 12:04:00",
"parentName": null
},
]
}
},
watch: {
$route(to, from) {
this.getActive()
}
},
methods: {
handleOpen(key, keyPath) {
console.log(key, keyPath);
// if (item.children.length == 0) {
// this.$router.push({path: item.url});
// }
},
handleSelect(key, keyPath) {
console.log(key, keyPath);
},
handleClose(key, keyPath) {
console.log(key, keyPath);
},
goUrlHome(item) {
console.log(item)
if (!item.children) {
this.$router.push({path: item.url});
}
},
goUrl(item) {
this.$router.push({path: item.url});
}
}
}
</script>
<style lang="scss" scoped>
.sidebar {
width: 240px;
height: 100%;
position: fixed;
box-shadow: 2px 0px 4px 1px rgba(153, 153, 153, 0.1600);
left: 0;
top: 0;
background: #fff;
.logo {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: nowrap;
flex-direction: row;
align-content: flex-start;
padding-top: 44px;
img {
width: 132px;
}
}
.sidebarList {
padding-top: 46px;
}
.sidebarList {
position: relative;
height: calc(100% - 110px);
overflow-y: scroll;
a {
text-decoration: none;
}
}
}
.title {
display: flex;
justify-content: flex-start;
align-items: center;
flex-wrap: nowrap;
flex-direction: row;
align-content: flex-start;
font-size: 16px;
font-family: Microsoft YaHei-Regular, Microsoft YaHei;
font-weight: 400;
color: #333333;
.icon {
display: flex;
margin-left: 40px;
justify-content: center;
align-items: center;
flex-wrap: nowrap;
flex-direction: row;
align-content: flex-start;
font-size: 20px;
}
}
.menus {
width: 100%;
margin: 0 auto;
::v-deep {
.el-menu-item-group__title {
padding: 0;
}
.el-menu {
border: none;
background: none;
}
.el-submenu {
margin-bottom: 0px;
}
.el-submenu__title {
width: 100%;
height: 50px;
box-shadow: 0px 6px 16px 0px rgba(1, 44, 76, 0);
font-size: 16px;
font-family: MicrosoftYaHei;
font-weight: 400;
color: #A5C0D4;
padding-left: 0 !important;
padding-right: 0 !important;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: nowrap;
flex-direction: row;
align-content: flex-start;
i {
color: #9db9ce;
}
}
.el-submenu .el-menu-item {
background: none;
color: #b3b3b3;
font-size: 16px;
margin-left: 40px;
padding-left: 36px !important;
height: 40px;
line-height: 40px;
}
.el-menu-item.is-active {
width: 200px;
height: 40px;
background: rgba(#609AFF, 0.1);
border-radius: 4px 4px 4px 4px;
font-size: 14px;
font-family: Microsoft YaHei-Regular, Microsoft YaHei;
font-weight: 400;
color: #327DFF;
position: relative;
.line {
width: 4px;
height: 40px;
background: #609AFF;
opacity: 0.9;
position: absolute;
right: 0;
top: 0;
}
}
.el-submenu__title:hover {
background: #eff5ff;
font-size: 16px;
font-family: MicrosoftYaHei;
font-weight: bold;
color: #a1a1a1;
span {
color: rgba(50, 125, 255, 1);
}
i {
color: #a1a1a1;
}
}
.el-submenu.active {
.el-submenu__icon-arrow {
display: none;
}
}
.is-opened.active .el-submenu__title {
background: #eff5ff;
}
.is-active .el-submenu__title, .is-opened .el-submenu__title {
font-size: 16px;
font-family: MicrosoftYaHei;
font-weight: bold;
color: #a1a1a1;
span {
color: rgba(50, 125, 255, 1);
}
i {
color: #a1a1a1;
}
}
}
}
.icons {
width: 20px;
height: 20px;
display: flex;
justify-content: center;
align-items: center;
flex-wrap: nowrap;
flex-direction: row;
align-content: flex-start;
margin-right: 16px;
}
.navInfo {
display: flex;
justify-content: flex-start;
align-items: center;
flex-wrap: nowrap;
flex-direction: row;
align-content: flex-start;
}
.icons.icon1 {
background: url("../assets/nav/icon_1.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon2 {
background: url("../assets/nav/icon_2.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon3 {
background: url("../assets/nav/icon_3.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon4 {
background: url("../assets/nav/icon_4.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon5 {
background: url("../assets/nav/icon_5.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon6 {
background: url("../assets/nav/icon_6.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon7 {
background: url("../assets/nav/icon_7.png") center center no-repeat;
background-size: 20px 20px;
}
.is-opened, .el-submenu__title:hover {
.icons.icon1 {
background: url("../assets/nav/icon_1_active.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon2 {
background: url("../assets/nav/icon_2_active.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon3 {
background: url("../assets/nav/icon_3_active.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon4 {
background: url("../assets/nav/icon_4_active.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon5 {
background: url("../assets/nav/icon_5_active.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon6 {
background: url("../assets/nav/icon_6_active.png") center center no-repeat;
background-size: 20px 20px;
}
.icons.icon7 {
background: url("../assets/nav/icon_7_active.png") center center no-repeat;
background-size: 20px 20px;
}
}
</style>