Skip to content

Commit

Permalink
feat: 🎸 新增固定一级菜单配置
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong502431556 committed Mar 29, 2021
1 parent 62eeb55 commit 4c4903e
Show file tree
Hide file tree
Showing 25 changed files with 362 additions and 59 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "vue-element-plus-admin",
"version": "0.0.5",
"version": "0.0.6",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
Expand Down
File renamed without changes.
File renamed without changes.
Expand Up @@ -36,23 +36,23 @@ export default defineComponent({
const levelList = ref<RouteRecordRaw[]>([])
function getBreadcrumb() {
let matched: any[] = currentRoute.value.matched.filter((item: RouteLocationMatched) => item.meta && item.meta.title)
const first = matched[0]
const matched: any[] = currentRoute.value.matched.filter((item: RouteLocationMatched) => item.meta && item.meta.title)
// const first = matched[0]
if (!isDashboard(first)) {
matched = [{ path: '/dashboard', meta: { title: '首页', icon: 'dashboard' }}].concat(matched)
}
// if (!isDashboard(first)) {
// matched = [{ path: '/dashboard', meta: { title: '首页', icon: 'dashboard' }}].concat(matched)
// }
levelList.value = matched.filter((item: RouteLocationMatched) => item.meta && item.meta.title && item.meta.breadcrumb !== false)
}
function isDashboard(route: RouteLocationMatched) {
const name = route && route.name
if (!name) {
return false
}
return (name as any).trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
}
// function isDashboard(route: RouteLocationMatched) {
// const name = route && route.name
// if (!name) {
// return false
// }
// return (name as any).trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
// }
function pathCompile(path: string): string {
const { params } = currentRoute.value
Expand Down
File renamed without changes.
File renamed without changes.
125 changes: 125 additions & 0 deletions src/pages/index/layout/components/MenuTab/index.vue
@@ -0,0 +1,125 @@
<template>
<el-tabs
v-model="activeName"
:tab-position="tabPosition"
@tab-click="changeTab"
>
<el-tab-pane
v-for="(item, $index) in tabRouters"
:key="$index"
:name="item.path === '/' ? '/dashboard' : item.path"
>
<template #label>
<div class="label-item">
<svg-icon :icon-class="filterTab(item, 'icon')" />
<div class="title-item">{{ filterTab(item, 'title') }}</div>
</div>
</template>
</el-tab-pane>
</el-tabs>
</template>

<script lang="ts">
import { defineComponent, ref, watch, onMounted, computed } from 'vue'
import { appStore } from '_@/store/modules/app'
import { permissionStore } from '_@/store/modules/permission'
import type { RouteRecordRaw } from 'vue-router'
import { useRouter } from 'vue-router'
import { findIndex } from '@/utils'
import { isExternal } from '@/utils/validate'
export default defineComponent({
name: 'MenuTab',
setup() {
const { currentRoute, push } = useRouter()
const activeName = ref<string>('')
const routers = computed((): RouteRecordRaw[] => permissionStore.routers)
const tabRouters = computed((): RouteRecordRaw[] => routers.value.filter(v => !v.meta?.hidden))
const layout = computed(() => appStore.layout)
const tabPosition = computed(() => layout.value === 'Classic' ? 'left' : 'top')
function init() {
const currentPath = currentRoute.value.fullPath.split('/')
const index = findIndex(tabRouters.value, (v: RouteRecordRaw) => {
if (v.path === '/') {
return `/${currentPath[1]}` === '/dashboard'
} else {
return v.path === `/${currentPath[1]}`
}
})
if (index > -1) {
activeName.value = `/${currentPath[1]}`
setActive(index)
permissionStore.SetAcitveTab(activeName.value)
}
}
function filterTab(item: RouteRecordRaw | any, key: string): any {
return item.meta && item.meta[key] ? item.meta[key] : item.children[0].meta[key]
}
function setActive(index: number): void {
const currRoute: any = tabRouters.value[index]
permissionStore.SetMenuTabRouters(currRoute.children)
}
function changeTab(item: any) {
const currRoute: any = tabRouters.value[item.index]
permissionStore.SetMenuTabRouters(currRoute.children)
if (isExternal(currRoute.children[0].path)) {
window.open(currRoute.children[0].path)
} else {
push(`${activeName.value === '/dashboard' ? '' : activeName.value}/${currRoute.children[0].path}`)
permissionStore.SetAcitveTab(activeName.value)
}
}
onMounted(() => {
init()
})
watch(
() => currentRoute.value,
() => {
init()
}
)
watch(
() => activeName.value,
(val) => {
permissionStore.SetAcitveTab(val)
}
)
return {
activeName,
tabRouters,
tabPosition,
filterTab,
setActive,
changeTab
}
}
})
</script>

<style lang="less" scoped>
.label-item {
height: 100%;
display: flex;
justify-content: center;
flex-wrap: wrap;
align-items: center;
&>div {
width: 100%;
}
.title-item {
position: relative;
top: -5px;
}
}
</style>
File renamed without changes.
Expand Up @@ -37,6 +37,11 @@
<!-- <div class="setting__title">界面功能</div> -->

<div class="setting__title">界面显示</div>
<div v-if="layout !== 'Top'" class="setting__item">
<span>固定一级菜单</span>
<el-switch v-model="showMenuTab" @change="setShowMenuTab" />
</div>

<div class="setting__item">
<span>固定Header</span>
<el-switch v-model="fixedHeader" @change="setFixedHeader" />
Expand Down Expand Up @@ -117,6 +122,7 @@ export default defineComponent({
if (mode === layout.value) return
appStore.SetLayout(mode)
appStore.SetCollapsed(false)
mode === 'Top' && appStore.SetShowMenuTab(false)
}
const fixedHeader = ref<boolean>(appStore.fixedHeader)
Expand Down Expand Up @@ -179,6 +185,11 @@ export default defineComponent({
appStore.SetShowBackTop(showBackTop)
}
const showMenuTab = ref<boolean>(appStore.showMenuTab)
function setShowMenuTab(showMenuTab: boolean) {
appStore.SetShowMenuTab(showMenuTab)
}
return {
drawer, toggleClick,
layout, setLayout,
Expand All @@ -193,7 +204,8 @@ export default defineComponent({
title, setTitle,
logoTitle, setLogoTitle,
greyMode, setGreyMode,
showBackTop, setShowBackTop
showBackTop, setShowBackTop,
showMenuTab, setShowMenuTab
}
}
})
Expand Down
File renamed without changes.
@@ -1,7 +1,7 @@
<template>
<template v-if="!item.meta?.hidden">
<template v-if="hasOneShowingChild(item.children, item) && (!onlyOneChild.children || onlyOneChild.noShowingChildren) && !item.meta?.alwaysShow">
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown': !isNest}">
<el-menu-item :index="resolvePath(onlyOneChild.path, showMenuTab ? `${activeTab === '/dashboard' ? '' : activeTab}/${basePath}` : '')" :class="{'submenu-title-noDropdown': !isNest}">
<item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon || (item.meta && item.meta.icon)" />
<template #title>
<span class="anticon-item">{{ onlyOneChild.meta.title }}</span>
Expand All @@ -14,7 +14,7 @@
:popper-class="layout !== 'Top'
? 'nest-popper-menu'
: 'top-popper-menu'"
:index="resolvePath(item.path)"
:index="resolvePath(item.path, showMenuTab ? `${activeTab === '/dashboard' ? '' : activeTab}/${basePath}` : '')"
>
<template #title>
<item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="item.meta.title" />
Expand All @@ -32,11 +32,13 @@
</template>
<script lang="ts">
import { defineComponent, PropType, ref } from 'vue'
import { defineComponent, PropType, ref, computed } from 'vue'
import type { RouteRecordRaw } from 'vue-router'
import path from 'path'
import { isExternal } from '@/utils/validate'
import Item from './Item.vue'
import { permissionStore } from '_@/store/modules/permission'
import { appStore } from '_@/store/modules/app'
export default defineComponent({
name: 'SiderItem',
components: { Item },
Expand All @@ -62,6 +64,9 @@ export default defineComponent({
setup(props) {
const onlyOneChild = ref<any>(null)
const activeTab = computed(() => permissionStore.activeTab)
const showMenuTab = computed(() => appStore.showMenuTab)
function hasOneShowingChild(children: RouteRecordRaw[] = [], parent: RouteRecordRaw): boolean {
const showingChildren: RouteRecordRaw[] = children.filter((item: RouteRecordRaw) => {
if (item.meta && item.meta.hidden) {
Expand All @@ -87,14 +92,16 @@ export default defineComponent({
return false
}
function resolvePath(routePath: string): string {
function resolvePath(routePath: string, otherPath: string): string {
if (isExternal(routePath)) {
return routePath
}
return path.resolve(props.basePath, routePath)
return path.resolve(otherPath || props.basePath, routePath)
}
return {
onlyOneChild,
activeTab,
showMenuTab,
hasOneShowingChild,
resolvePath
}
Expand Down
Expand Up @@ -12,7 +12,7 @@
@select="selectMenu"
>
<sider-item
v-for="route in routers"
v-for="route in showMenuTab ? menuTabRouters : routers"
:key="route.path"
:item="route"
:layout="layout"
Expand Down Expand Up @@ -62,7 +62,12 @@ export default defineComponent({
const collapsed = computed(() => appStore.collapsed)
const showLogo = computed(() => appStore.showLogo)
const showMenuTab = computed(() => appStore.showMenuTab)
const menuTabRouters = computed(() => permissionStore.menuTabRouters)
const activeTab = computed(() => permissionStore.activeTab)
function selectMenu(path: string) {
if (currentRoute.value.fullPath === path) return
if (isExternal(path)) {
window.open(path)
} else {
Expand All @@ -75,6 +80,9 @@ export default defineComponent({
activeMenu,
collapsed,
showLogo,
showMenuTab,
menuTabRouters,
activeTab,
variables,
selectMenu
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 4c4903e

Please sign in to comment.