| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import { createRouter, createWebHashHistory } from 'vue-router'
- import { routes } from './config'
- import { computed, watch, watchEffect } from 'vue'
- import { RoutesName } from './constant'
- import { metas } from './constant'
- import { RouteRaw } from './config'
- export const history = createWebHashHistory()
- export const router = createRouter({ history, routes })
- export const getRouteTree = (name: RoutesName, raw: RouteRaw[] = routes): void | RouteRaw => {
- for (const route of raw) {
- if (route.name === name) {
- return route
- } else if (route.children) {
- const children = getRouteTree(name, route.children)
- if (children) {
- return {...route, children: [children] }
- }
- }
- }
- }
- export const getRouteNames = (name: RoutesName, raw: RouteRaw[] = routes): RoutesName[] => {
- let current = getRouteTree(name, raw)
- const names: RoutesName[] = []
- while (current) {
- names.push(current.name!)
- current = current.children && current.children[0]
- }
- return names
- }
- export const getRouteConfig = (name: RoutesName, raw: RouteRaw[] = routes): RouteRaw | void => {
- let current = getRouteTree(name, raw)
- while (current?.children && current.children[0]) {
- current = current.children[0]
- }
- return current
- }
- export const currentRouteNames = computed(() => getRouteNames(router.currentRoute.value.name as RoutesName, routes))
- export const currentLayout = computed(() => {
- const names = currentRouteNames.value
- const layoutNames = [RoutesName.fuseEditSwitch, RoutesName.show, RoutesName.error, RoutesName.sceneEdit, RoutesName.signModel] as const
- return layoutNames.find(name => names.includes(name))
- })
- export const currentMeta = computed(() => {
- const currentName = router.currentRoute.value.name
- if (currentName && currentName in metas) {
- return (metas as any )[currentName] as ((typeof metas)[keyof typeof metas])
- }
- })
- let timeout: any
- watch(currentLayout, () => {
- clearTimeout(timeout)
- timeout = setTimeout(() => {
- if (!currentLayout.value) {
- router.replace({ name: RoutesName.fireInfo })
- }
- }, 100)
- })
- export * from './config'
- export * from './constant'
- export default router
|