优化topNav打开菜单

This commit is contained in:
不做码农 2023-02-09 11:49:08 +08:00
parent 5ce9ec92b1
commit 6d9b76e5b1

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
} }