From 66d1bfea3cf02afcfe29729d70e61e0722a7aae4 Mon Sep 17 00:00:00 2001 From: arlo Date: Sun, 14 Jan 2024 15:47:44 +0800 Subject: [PATCH] fix(kit): filter useless route info, closes #159 --- .../devtools-kit/src/core/router/index.ts | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/devtools-kit/src/core/router/index.ts b/packages/devtools-kit/src/core/router/index.ts index 6f9779fc..0edecb63 100644 --- a/packages/devtools-kit/src/core/router/index.ts +++ b/packages/devtools-kit/src/core/router/index.ts @@ -1,4 +1,4 @@ -import type { RouteLocationNormalizedLoaded, RouteRecordNormalized, Router } from 'vue-router' +import type { RouteLocationNormalizedLoaded, RouteRecordNormalized, RouteRecordRaw, Router } from 'vue-router' import type { AppRecord } from '@vue/devtools-schema' import { deepClone, target as global } from '@vue/devtools-shared' import { debounce } from 'perfect-debounce' @@ -34,17 +34,39 @@ export function normalizeRouterInfo(appRecord: AppRecord) { const routesMap = new Map() return (router?.getRoutes() || []).filter(i => !routesMap.has(i.path) && routesMap.set(i.path, 1)) } - function init() { - const router = appRecord.app?.config.globalProperties.$router as Router | undefined - const currentRoute = router?.currentRoute.value - const routes = getRoutes(router).map((item) => { - const { path, name, children } = item + function filterRoutes(routes: RouteRecordRaw[]) { + return routes.map((item) => { + let { path, name, children } = item + if (children?.length) + children = filterRoutes(children) + return { path, name, children, } }) + } + function filterCurrentRoute(route: RouteLocationNormalizedLoaded & { href?: string } | undefined) { + if (route) { + const { fullPath, hash, href, path, name, matched, params, query } = route + return { + fullPath, + hash, + href, + path, + name, + params, + query, + matched: filterRoutes(matched), + } + } + return route + } + function init() { + const router = appRecord.app?.config.globalProperties.$router as Router | undefined + const currentRoute = filterCurrentRoute(router?.currentRoute.value) + const routes = filterRoutes(getRoutes(router)) const c = console.warn console.warn = () => {} global[RouterInfoKey] = {