feat:菜单栏添加icon支持
This commit is contained in:
parent
5fcc47816c
commit
4929bf7d03
@ -1,13 +1,15 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import { computed } from 'vue'
|
import { computed, h } from 'vue'
|
||||||
import { usePermissionStore } from '@/store/modules/permission'
|
import { usePermissionStore } from '@/store/modules/permission'
|
||||||
|
|
||||||
|
import { NIcon } from 'naive-ui'
|
||||||
|
import { ListAlt, CircleRegular } from '@vicons/fa'
|
||||||
|
|
||||||
import { isExternal } from '@/utils/is'
|
import { isExternal } from '@/utils/is'
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const permissionStore = usePermissionStore()
|
const permissionStore = usePermissionStore()
|
||||||
|
|
||||||
const { currentRoute } = router
|
const { currentRoute } = router
|
||||||
|
|
||||||
const menuOptions = computed(() => {
|
const menuOptions = computed(() => {
|
||||||
@ -25,6 +27,25 @@ function resolvePath(basePath, path) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function renderIcon(icon, props = { size: 8 }) {
|
||||||
|
return () => h(NIcon, { ...props }, { default: () => h(icon) })
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSingleRoute(route) {
|
||||||
|
let isSingle = true
|
||||||
|
let curRoute = route
|
||||||
|
while (curRoute.children && curRoute.children.length) {
|
||||||
|
if (curRoute.children.length > 1) {
|
||||||
|
isSingle = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if (curRoute.children.length === 1) {
|
||||||
|
curRoute = curRoute.children[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isSingle
|
||||||
|
}
|
||||||
|
|
||||||
function generateOptions(routes, basePath) {
|
function generateOptions(routes, basePath) {
|
||||||
let options = []
|
let options = []
|
||||||
routes.forEach((route) => {
|
routes.forEach((route) => {
|
||||||
@ -35,13 +56,10 @@ function generateOptions(routes, basePath) {
|
|||||||
path: resolvePath(basePath, route.path),
|
path: resolvePath(basePath, route.path),
|
||||||
}
|
}
|
||||||
if (route.children && route.children.length) {
|
if (route.children && route.children.length) {
|
||||||
|
curOption.icon = renderIcon(route.meta?.icon || ListAlt, { size: 16 })
|
||||||
curOption.children = generateOptions(route.children, resolvePath(basePath, route.path))
|
curOption.children = generateOptions(route.children, resolvePath(basePath, route.path))
|
||||||
}
|
} else {
|
||||||
if (curOption.children && curOption.children.length <= 1) {
|
curOption.icon = renderIcon(route.meta?.icon || CircleRegular)
|
||||||
if (curOption.children.length === 1) {
|
|
||||||
curOption = { ...curOption.children[0] }
|
|
||||||
}
|
|
||||||
delete curOption.children
|
|
||||||
}
|
}
|
||||||
options.push(curOption)
|
options.push(curOption)
|
||||||
}
|
}
|
||||||
@ -67,7 +85,9 @@ function handleMenuSelect(key, item) {
|
|||||||
<template>
|
<template>
|
||||||
<n-menu
|
<n-menu
|
||||||
class="side-menu"
|
class="side-menu"
|
||||||
:root-indent="20"
|
accordion
|
||||||
|
:indent="12"
|
||||||
|
:root-indent="12"
|
||||||
:options="menuOptions"
|
:options="menuOptions"
|
||||||
:value="(currentRoute.meta && currentRoute.meta.activeMenu) || currentRoute.name"
|
:value="(currentRoute.meta && currentRoute.meta.activeMenu) || currentRoute.name"
|
||||||
@update:value="handleMenuSelect"
|
@update:value="handleMenuSelect"
|
||||||
@ -111,55 +131,7 @@ function handleMenuSelect(key, item) {
|
|||||||
font-weight: normal;
|
font-weight: normal;
|
||||||
position: relative;
|
position: relative;
|
||||||
overflow: visible !important;
|
overflow: visible !important;
|
||||||
&::before {
|
|
||||||
content: '';
|
|
||||||
position: absolute;
|
|
||||||
left: -15px;
|
|
||||||
top: 0;
|
|
||||||
bottom: 0;
|
|
||||||
margin: auto;
|
|
||||||
width: 5px;
|
|
||||||
height: 5px;
|
|
||||||
border-radius: 50%;
|
|
||||||
border: 1px solid #333;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// .side-menu {
|
|
||||||
// // padding-left: 15px;
|
|
||||||
// .n-menu-item-content-header {
|
|
||||||
// color: #fff !important;
|
|
||||||
// font-weight: bold;
|
|
||||||
// font-size: 14px;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// .n-submenu {
|
|
||||||
// .n-menu-item-content-header {
|
|
||||||
// color: #fff !important;
|
|
||||||
// font-weight: bold;
|
|
||||||
// font-size: 14px;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// .n-submenu-children {
|
|
||||||
// .n-menu-item-content-header {
|
|
||||||
// color: #fff !important;
|
|
||||||
// font-weight: normal;
|
|
||||||
// font-size: 12px;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// .n-menu-item {
|
|
||||||
// border-top-left-radius: 5px;
|
|
||||||
// border-bottom-left-radius: 5px;
|
|
||||||
// &:hover,
|
|
||||||
// &.n-menu-item--selected::before {
|
|
||||||
// background-color: #16243a;
|
|
||||||
// right: 0;
|
|
||||||
// left: 0;
|
|
||||||
// border-right: 3px solid $primaryColor;
|
|
||||||
// background-color: unset !important;
|
|
||||||
// background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba($primaryColor, 0.3) 100%);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
</style>
|
</style>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import Layout from '@/layout/index.vue'
|
import Layout from '@/layout/index.vue'
|
||||||
import Dashboard from '@/views/dashboard/index.vue'
|
import Dashboard from '@/views/dashboard/index.vue'
|
||||||
|
import { Dove, Github, HouseDamage, Link } from '@vicons/fa'
|
||||||
|
|
||||||
export const basicRoutes = [
|
export const basicRoutes = [
|
||||||
{
|
{
|
||||||
@ -38,6 +39,7 @@ export const basicRoutes = [
|
|||||||
redirect: '/dashboard',
|
redirect: '/dashboard',
|
||||||
meta: {
|
meta: {
|
||||||
title: '首页',
|
title: '首页',
|
||||||
|
icon: HouseDamage,
|
||||||
},
|
},
|
||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
@ -51,33 +53,13 @@ export const basicRoutes = [
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
|
||||||
name: 'ERROR-PAGE',
|
|
||||||
path: '/error-page',
|
|
||||||
component: Layout,
|
|
||||||
redirect: '/error-page/404',
|
|
||||||
meta: {
|
|
||||||
title: '错误页',
|
|
||||||
},
|
|
||||||
children: [
|
|
||||||
{
|
|
||||||
name: 'ERROR-404',
|
|
||||||
path: '404',
|
|
||||||
component: () => import('@/views/error-page/404.vue'),
|
|
||||||
meta: {
|
|
||||||
title: '404',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
{
|
||||||
name: 'TEST',
|
name: 'TEST',
|
||||||
path: '/test',
|
path: '/test',
|
||||||
component: Layout,
|
component: Layout,
|
||||||
redirect: '/test/unocss',
|
redirect: '/test/unocss',
|
||||||
meta: {
|
meta: {
|
||||||
title: '测试',
|
title: '基础功能测试',
|
||||||
},
|
},
|
||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
@ -116,12 +98,33 @@ export const basicRoutes = [
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: 'ERROR-PAGE',
|
||||||
|
path: '/error-page',
|
||||||
|
component: Layout,
|
||||||
|
redirect: '/error-page/404',
|
||||||
|
meta: {
|
||||||
|
title: '错误页',
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
name: 'ERROR-404',
|
||||||
|
path: '404',
|
||||||
|
component: () => import('@/views/error-page/404.vue'),
|
||||||
|
meta: {
|
||||||
|
title: '404',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: 'EXTERNAL-LINK',
|
name: 'EXTERNAL-LINK',
|
||||||
path: '/external-link',
|
path: '/external-link',
|
||||||
component: Layout,
|
component: Layout,
|
||||||
meta: {
|
meta: {
|
||||||
title: '外部链接',
|
title: '外部链接',
|
||||||
|
icon: Link,
|
||||||
},
|
},
|
||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
@ -129,6 +132,7 @@ export const basicRoutes = [
|
|||||||
path: 'https://github.com/zclzone/vue-naive-admin',
|
path: 'https://github.com/zclzone/vue-naive-admin',
|
||||||
meta: {
|
meta: {
|
||||||
title: '源码 - github',
|
title: '源码 - github',
|
||||||
|
icon: Github,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -143,6 +147,7 @@ export const basicRoutes = [
|
|||||||
path: 'https://www.yuque.com/qszone/vue-naive-admin',
|
path: 'https://www.yuque.com/qszone/vue-naive-admin',
|
||||||
meta: {
|
meta: {
|
||||||
title: '文档 - 语雀',
|
title: '文档 - 语雀',
|
||||||
|
icon: Dove,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
Loading…
Reference in New Issue
Block a user