优化topNav打开菜单

This commit is contained in:
不做码农
2023-02-09 11:49:08 +08:00
parent 5ce9ec92b1
commit 6d9b76e5b1
+19 -30
View File
@@ -43,10 +43,10 @@ import usePermissionStore from '@/store/modules/permission'
// 顶部栏初始数 // 顶部栏初始数
const visibleNumber = ref(5) const visibleNumber = ref(5)
// 是否为首次加载
const isFrist = ref(false)
// 当前激活菜单的 index // 当前激活菜单的 index
const currentIndex = ref(undefined) const currentIndex = ref(undefined)
// 隐藏侧边栏路由
const hideList = ['/index', '/user/profile']
const appStore = useAppStore() const appStore = useAppStore()
const settingsStore = useSettingsStore() const settingsStore = useSettingsStore()
@@ -82,10 +82,10 @@ const childrenMenus = computed(() => {
for (let item in router.children) { for (let item in router.children) {
if (router.children[item].parentPath === undefined) { if (router.children[item].parentPath === undefined) {
if (router.path === '/') { if (router.path === '/') {
router.children[item].path = getNormalPath('/redirect/' + router.children[item].path) router.children[item].path = '/' + router.children[item].path
} else { } else {
if (!isHttp(router.children[item].path)) { if (!isHttp(router.children[item].path)) {
router.children[item].path = getNormalPath(router.path + '/' + router.children[item].path) router.children[item].path = router.path + '/' + router.children[item].path
} }
} }
router.children[item].parentPath = router.path router.children[item].parentPath = router.path
@@ -99,52 +99,39 @@ const childrenMenus = computed(() => {
// 默认激活的菜单 // 默认激活的菜单
const activeMenu = computed(() => { const activeMenu = computed(() => {
const path = route.path const path = route.path
let activePath = defaultRouter.value let activePath = path
if (path.lastIndexOf('/') > 0) { if (path !== undefined && path.lastIndexOf('/') > 0 && hideList.indexOf(path) === -1) {
const tmpPath = path.substring(1, path.length) const tmpPath = path.substring(1, path.length)
activePath = '/' + tmpPath.substring(0, tmpPath.indexOf('/')) activePath = '/' + tmpPath.substring(0, tmpPath.indexOf('/'))
} else if ('/index' == path || '' == path) { if (!route.meta.link) {
if (!isFrist.value) { appStore.toggleSideBarHide(false)
isFrist.value = true
} else {
activePath = 'index'
} }
} else if (!route.children) {
activePath = path
appStore.toggleSideBarHide(true)
} }
let routes = activeRoutes(activePath)
if (routes.length === 0) {
activePath = currentIndex.value || defaultRouter.value
activeRoutes(activePath) activeRoutes(activePath)
}
return activePath return activePath
}) })
// 默认激活的路由
const defaultRouter = computed(() => {
let router
Object.keys(routers.value).some((key) => {
if (!routers.value[key].hidden) {
router = routers.value[key].path
return true
}
})
return router
})
function setVisibleNumber() { function setVisibleNumber() {
const width = document.body.getBoundingClientRect().width / 3 const width = document.body.getBoundingClientRect().width / 3
visibleNumber.value = parseInt(width / 85) visibleNumber.value = parseInt(width / 85)
} }
function handleSelect(key, keyPath) { function handleSelect(key, keyPath) {
currentIndex.value = key currentIndex.value = key
const route = routers.value.find((item) => item.path === key)
if (isHttp(key)) { if (isHttp(key)) {
// http(s):// 路径新窗口打开 // http(s):// 路径新窗口打开
window.open(key, '_blank') window.open(key, '_blank')
} else if (key.indexOf('/redirect') !== -1) { } else if (!route || !route.children) {
// /redirect 路径内部打开 // 没有子路由路径内部打开
router.push({ path: key.replace('/redirect', '') }).catch((err) => {}) router.push({ path: key })
appStore.toggleSideBarHide(true)
} else { } else {
// 显示左侧联动菜单 // 显示左侧联动菜单
activeRoutes(key) activeRoutes(key)
appStore.toggleSideBarHide(false)
} }
} }
// 当前激活的路由 // 当前激活的路由
@@ -159,6 +146,8 @@ function activeRoutes(key) {
} }
if (routes.length > 0) { if (routes.length > 0) {
permissionStore.setSidebarRouters(routes) permissionStore.setSidebarRouters(routes)
} else {
// appStore.toggleSideBarHide(true)
} }
return routes return routes
} }