fix topnav一级导航跳转失败

This commit is contained in:
zory 2022-04-19 20:38:44 +08:00
parent bdc65fbfe9
commit a4089d4499
2 changed files with 77 additions and 70 deletions

View File

@ -21,137 +21,142 @@
</template>
<script setup>
import { constantRoutes } from "@/router";
import { isHttp } from "@/utils/validate";
import { constantRoutes } from '@/router'
import { isHttp } from '@/utils/validate'
import { useRouter } from 'vue-router'
//
const visibleNumber = ref(5);
const visibleNumber = ref(5)
//
const isFrist = ref(false);
const isFrist = ref(false)
// index
const currentIndex = ref(null);
const currentIndex = ref(undefined)
const store = useStore();
const route = useRoute();
const store = useStore()
const route = useRoute()
const router = useRouter()
//
const theme = computed(() => store.state.settings.theme);
const theme = computed(() => store.state.settings.theme)
//
const routers = computed(() => store.state.permission.topbarRouters);
const routers = computed(() => store.state.permission.topbarRouters)
//
const topMenus = computed(() => {
let topMenus = [];
let topMenus = []
routers.value.map((menu) => {
if (menu.hidden !== true) {
//
if (menu.path === "/") {
topMenus.push(menu.children[0]);
if (menu.path === '/') {
topMenus.push(menu.children[0])
} else {
topMenus.push(menu);
topMenus.push(menu)
}
}
});
return topMenus;
});
})
return topMenus
})
//
const childrenMenus = computed(() => {
let childrenMenus = [];
let childrenMenus = []
routers.value.map((router) => {
for (let item in router.children) {
if (router.children[item].parentPath === undefined) {
if (router.path === "/") {
router.children[item].path =
"/redirect/" + router.children[item].path;
if (router.path === '/') {
router.children[item].path = '/redirect/' + router.children[item].path
} else {
if (!isHttp(router.children[item].path)) {
router.children[item].path =
router.path + "/" + router.children[item].path;
router.path + '/' + router.children[item].path
}
}
router.children[item].parentPath = router.path;
router.children[item].parentPath = router.path
}
childrenMenus.push(router.children[item]);
childrenMenus.push(router.children[item])
}
});
return constantRoutes.concat(childrenMenus);
});
})
return constantRoutes.concat(childrenMenus)
})
//
const activeMenu = computed(() => {
const path = route.path;
let activePath = defaultRouter.value;
if (path.lastIndexOf("/") > 0) {
const tmpPath = path.substring(1, path.length);
activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
} else if ("/index" == path || "" == path) {
const path = route.path
let activePath = defaultRouter.value
if (path.lastIndexOf('/') > 0) {
const tmpPath = path.substring(1, path.length)
activePath = '/' + tmpPath.substring(0, tmpPath.indexOf('/'))
} else if ('/index' == path || '' == path) {
if (!isFrist.value) {
isFrist.value = true;
isFrist.value = true
} else {
activePath = "index";
activePath = 'index'
}
}
let routes = activeRoutes(activePath);
let routes = activeRoutes(activePath)
if (routes.length === 0) {
activePath = currentIndex.value || defaultRouter.value;
activeRoutes(activePath);
activePath = currentIndex.value || defaultRouter.value
console.log('activePath', activePath)
activeRoutes(activePath)
}
return activePath;
});
return activePath
})
//
const defaultRouter = computed(() => {
let router;
let router
Object.keys(routers.value).some((key) => {
if (!routers.value[key].hidden) {
router = routers.value[key].path;
return true;
router = routers.value[key].path
return true
}
});
return router;
});
})
return router
})
function setVisibleNumber() {
const width = document.body.getBoundingClientRect().width / 3;
visibleNumber.value = parseInt(width / 85);
const width = document.body.getBoundingClientRect().width / 3
visibleNumber.value = parseInt(width / 85)
}
function handleSelect(key, keyPath) {
currentIndex.value = key;
currentIndex.value = key
if (isHttp(key)) {
// http(s)://
window.open(key, "_blank");
} else if (key.indexOf("/redirect") !== -1) {
window.open(key, '_blank')
} else if (key.indexOf('/redirect') !== -1) {
// /redirect
router.push({ path: key.replace("/redirect", "") }).catch(err => {});
} else {
//
activeRoutes(key);
activeRoutes(key)
}
}
//
function activeRoutes(key) {
let routes = [];
var routes = []
if (childrenMenus.value && childrenMenus.value.length > 0) {
childrenMenus.value.map((item) => {
if (key == item.parentPath || (key == "index" && "" == item.path)) {
routes.push(item);
if (key == item.parentPath || (key == 'index' && '' == item.path)) {
routes.push(item)
}
});
})
}
if (routes.length > 0) {
store.commit("SET_SIDEBAR_ROUTERS", routes);
store.commit('SET_SIDEBAR_ROUTERS', routes)
}
return routes;
return routes
}
onMounted(() => {
window.addEventListener("resize", setVisibleNumber);
});
window.addEventListener('resize', setVisibleNumber)
})
onBeforeUnmount(() => {
window.removeEventListener("resize", setVisibleNumber);
});
window.removeEventListener('resize', setVisibleNumber)
})
onMounted(() => {
setVisibleNumber();
});
setVisibleNumber()
})
</script>
<style lang="scss">

View File

@ -39,14 +39,14 @@ const permission = {
getRouters().then(res => {
const sdata = JSON.parse(JSON.stringify(res.data))
const rdata = JSON.parse(JSON.stringify(res.data))
const defaultData = JSON.parse(JSON.stringify(res.data))
//const defaultData = JSON.parse(JSON.stringify(res.data))
const sidebarRoutes = filterAsyncRouter(sdata)
const rewriteRoutes = filterAsyncRouter(rdata, false, true)
const defaultRoutes = filterAsyncRouter(defaultData)
//const defaultRoutes = filterAsyncRouter(defaultData)
commit('SET_ROUTES', rewriteRoutes)
commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
commit('SET_DEFAULT_ROUTES', sidebarRoutes)
commit('SET_TOPBAR_ROUTES', defaultRoutes)
commit('SET_TOPBAR_ROUTES', sidebarRoutes)
resolve(rewriteRoutes)
})
})
@ -66,9 +66,11 @@ function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
route.component = Layout
} else if (route.component === 'ParentView') {
route.component = ParentView
} else if (route.component === 'InnerLink') {
route.component = InnerLink
} else {
}
// else if (route.component === 'InnerLink') {
// route.component = InnerLink
// }
else {
route.component = loadView(route.component)
}
}