|
@@ -1,48 +1,52 @@
|
|
-import { useEmitLeave } from '@/hook/useEdit'
|
|
|
|
-import { isEdit } from '@/store'
|
|
|
|
-import { ref, watch } from 'vue'
|
|
|
|
-import { Router } from 'vue-router'
|
|
|
|
|
|
+import { useEmitLeave } from "@/hook/useEdit";
|
|
|
|
+import { isEdit } from "@/store";
|
|
|
|
+import { ref, watch } from "vue";
|
|
|
|
+import { Router } from "vue-router";
|
|
import {
|
|
import {
|
|
createRouter,
|
|
createRouter,
|
|
createWebHashHistory,
|
|
createWebHashHistory,
|
|
- RouteLocationNormalizedLoaded
|
|
|
|
-} from 'vue-router'
|
|
|
|
-import type { RoutesRef, RoutesRaw, RouteAtom } from './info'
|
|
|
|
|
|
+ RouteLocationNormalizedLoaded,
|
|
|
|
+} from "vue-router";
|
|
|
|
+import type { RoutesRef, RoutesRaw, RouteAtom } from "./info";
|
|
|
|
+import { readyRouteName } from "./constant";
|
|
|
|
|
|
export const router = createRouter({
|
|
export const router = createRouter({
|
|
history: createWebHashHistory(),
|
|
history: createWebHashHistory(),
|
|
- routes: []
|
|
|
|
-})
|
|
|
|
|
|
+ routes: [],
|
|
|
|
+});
|
|
|
|
|
|
router.beforeEach((next, from) => {
|
|
router.beforeEach((next, from) => {
|
|
|
|
+ if (!next.name) {
|
|
|
|
+ router.replace({ name: readyRouteName.scene });
|
|
|
|
+ }
|
|
if (next?.meta?.title) {
|
|
if (next?.meta?.title) {
|
|
- document.title = next?.meta?.title
|
|
|
|
|
|
+ document.title = next?.meta?.title;
|
|
}
|
|
}
|
|
if (!isEdit.value || from.name === next.name) {
|
|
if (!isEdit.value || from.name === next.name) {
|
|
- return true
|
|
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
- return useEmitLeave(false)
|
|
|
|
-})
|
|
|
|
|
|
+ return useEmitLeave(false);
|
|
|
|
+});
|
|
|
|
|
|
const analysisRouter = (
|
|
const analysisRouter = (
|
|
router: Router,
|
|
router: Router,
|
|
newRoutes: RoutesRaw,
|
|
newRoutes: RoutesRaw,
|
|
oldRoutes: RoutesRaw = [],
|
|
oldRoutes: RoutesRaw = [],
|
|
- parentName: string = ''
|
|
|
|
|
|
+ parentName: string = ""
|
|
) => {
|
|
) => {
|
|
for (const oldRoute of oldRoutes) {
|
|
for (const oldRoute of oldRoutes) {
|
|
if (newRoutes.every(({ name }) => name !== oldRoute.name)) {
|
|
if (newRoutes.every(({ name }) => name !== oldRoute.name)) {
|
|
- router.removeRoute(oldRoute.name)
|
|
|
|
|
|
+ router.removeRoute(oldRoute.name);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
for (const newRoute of newRoutes) {
|
|
for (const newRoute of newRoutes) {
|
|
- const oldRoute = oldRoutes.find(({ name }) => name === newRoute.name)
|
|
|
|
|
|
+ const oldRoute = oldRoutes.find(({ name }) => name === newRoute.name);
|
|
if (!oldRoute) {
|
|
if (!oldRoute) {
|
|
if (parentName) {
|
|
if (parentName) {
|
|
- router.addRoute(parentName, newRoute as any)
|
|
|
|
|
|
+ router.addRoute(parentName, newRoute as any);
|
|
} else {
|
|
} else {
|
|
- router.addRoute(newRoute as any)
|
|
|
|
|
|
+ router.addRoute(newRoute as any);
|
|
}
|
|
}
|
|
} else if (newRoute.children) {
|
|
} else if (newRoute.children) {
|
|
analysisRouter(
|
|
analysisRouter(
|
|
@@ -50,10 +54,10 @@ const analysisRouter = (
|
|
newRoute.children,
|
|
newRoute.children,
|
|
oldRoute.children,
|
|
oldRoute.children,
|
|
newRoute.name
|
|
newRoute.name
|
|
- )
|
|
|
|
|
|
+ );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+};
|
|
|
|
|
|
const flatRouters = (
|
|
const flatRouters = (
|
|
routes: RoutesRaw,
|
|
routes: RoutesRaw,
|
|
@@ -61,74 +65,74 @@ const flatRouters = (
|
|
parent?: RouteAtom
|
|
parent?: RouteAtom
|
|
) => {
|
|
) => {
|
|
for (const route of routes) {
|
|
for (const route of routes) {
|
|
- let path = route.path
|
|
|
|
- if (parent && route.path[0] !== '/') {
|
|
|
|
- const ppath = parent.path
|
|
|
|
- path = `${ppath}${ppath[ppath.length - 1] === '/' ? '' : '/'}${path}`
|
|
|
|
|
|
+ let path = route.path;
|
|
|
|
+ if (parent && route.path[0] !== "/") {
|
|
|
|
+ const ppath = parent.path;
|
|
|
|
+ path = `${ppath}${ppath[ppath.length - 1] === "/" ? "" : "/"}${path}`;
|
|
}
|
|
}
|
|
|
|
|
|
flat.push({
|
|
flat.push({
|
|
...route,
|
|
...route,
|
|
- path
|
|
|
|
- })
|
|
|
|
|
|
+ path,
|
|
|
|
+ });
|
|
if (route.children) {
|
|
if (route.children) {
|
|
- flatRouters(route.children, flat, route)
|
|
|
|
|
|
+ flatRouters(route.children, flat, route);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return flat
|
|
|
|
-}
|
|
|
|
|
|
+ return flat;
|
|
|
|
+};
|
|
|
|
|
|
// 动态更改路由
|
|
// 动态更改路由
|
|
export const setupRouter = (routesRef: RoutesRef) => {
|
|
export const setupRouter = (routesRef: RoutesRef) => {
|
|
- let routes = ref<RoutesRaw>()
|
|
|
|
- let defRouteName
|
|
|
|
- let unSetName: string
|
|
|
|
|
|
+ let routes = ref<RoutesRaw>();
|
|
|
|
+ let defRouteName;
|
|
|
|
+ let unSetName: string;
|
|
const exists = (routes: RoutesRaw, route: RouteLocationNormalizedLoaded) => {
|
|
const exists = (routes: RoutesRaw, route: RouteLocationNormalizedLoaded) => {
|
|
- return routes.find(({ path }) => path === route.path)
|
|
|
|
- }
|
|
|
|
|
|
+ return routes.find(({ path }) => path === route.path);
|
|
|
|
+ };
|
|
|
|
|
|
watch(
|
|
watch(
|
|
routesRef,
|
|
routesRef,
|
|
({ list: newRoutes, default: _defRouteName }, oldRoutesRef) => {
|
|
({ list: newRoutes, default: _defRouteName }, oldRoutesRef) => {
|
|
- const oldRoutes = oldRoutesRef?.list || []
|
|
|
|
- analysisRouter(router, newRoutes, oldRoutes)
|
|
|
|
- routes.value = flatRouters(newRoutes)
|
|
|
|
- defRouteName = _defRouteName
|
|
|
|
|
|
+ const oldRoutes = oldRoutesRef?.list || [];
|
|
|
|
+ analysisRouter(router, newRoutes, oldRoutes);
|
|
|
|
+ routes.value = flatRouters(newRoutes);
|
|
|
|
+ defRouteName = _defRouteName;
|
|
|
|
|
|
if (!exists(routes.value, router.currentRoute.value)) {
|
|
if (!exists(routes.value, router.currentRoute.value)) {
|
|
// router.replace({ name: defRouteName })
|
|
// router.replace({ name: defRouteName })
|
|
}
|
|
}
|
|
},
|
|
},
|
|
- { flush: 'post', immediate: true }
|
|
|
|
- )
|
|
|
|
|
|
+ { flush: "post", immediate: true }
|
|
|
|
+ );
|
|
|
|
|
|
- let enter = 0
|
|
|
|
|
|
+ let enter = 0;
|
|
const stopWatchCurrent = watch([router.currentRoute], () => {
|
|
const stopWatchCurrent = watch([router.currentRoute], () => {
|
|
if (!routes.value?.length) {
|
|
if (!routes.value?.length) {
|
|
- return
|
|
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
if (!exists(routes.value, router.currentRoute.value)) {
|
|
if (!exists(routes.value, router.currentRoute.value)) {
|
|
- unSetName = router.currentRoute.value.fullPath
|
|
|
|
- enter++
|
|
|
|
|
|
+ unSetName = router.currentRoute.value.fullPath;
|
|
|
|
+ enter++;
|
|
// router.replace({ name: defRouteName })
|
|
// router.replace({ name: defRouteName })
|
|
} else {
|
|
} else {
|
|
- enter--
|
|
|
|
|
|
+ enter--;
|
|
}
|
|
}
|
|
- })
|
|
|
|
|
|
+ });
|
|
|
|
|
|
const stopWatchRoutes = watch(routes, () => {
|
|
const stopWatchRoutes = watch(routes, () => {
|
|
if (!routes.value?.length) {
|
|
if (!routes.value?.length) {
|
|
- return
|
|
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
if (unSetName && !enter) {
|
|
if (unSetName && !enter) {
|
|
- const route = exists(routes.value, { path: unSetName } as any)
|
|
|
|
|
|
+ const route = exists(routes.value, { path: unSetName } as any);
|
|
if (route) {
|
|
if (route) {
|
|
- router.replace({ name: route.name })
|
|
|
|
- stopWatchCurrent()
|
|
|
|
- stopWatchRoutes()
|
|
|
|
|
|
+ router.replace({ name: route.name });
|
|
|
|
+ stopWatchCurrent();
|
|
|
|
+ stopWatchRoutes();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- })
|
|
|
|
-}
|
|
|
|
-export * from './constant'
|
|
|
|
-export default router
|
|
|
|
|
|
+ });
|
|
|
|
+};
|
|
|
|
+export * from "./constant";
|
|
|
|
+export default router;
|