From 1eed20ebce96b65de60795bbd8bc33f64174cc75 Mon Sep 17 00:00:00 2001 From: xiaoxian521 <1923740402@qq.com> Date: Thu, 8 Apr 2021 17:44:23 +0800 Subject: [PATCH] feat: add tags --- src/components/breadCrumb/index.vue | 12 +-- src/layout/components/AppMain.vue | 1 - src/layout/components/index.ts | 1 + src/layout/components/sidebar/index.vue | 14 +++ src/layout/components/tag/index.vue | 132 ++++++++++++++++++------ src/layout/components/tag/tagsHook.ts | 74 +++++++++++++ src/layout/index.vue | 5 +- src/router/index.ts | 1 + 8 files changed, 199 insertions(+), 41 deletions(-) create mode 100644 src/layout/components/tag/tagsHook.ts diff --git a/src/components/breadCrumb/index.vue b/src/components/breadCrumb/index.vue index 4d829b852..6bff0a430 100644 --- a/src/components/breadCrumb/index.vue +++ b/src/components/breadCrumb/index.vue @@ -60,11 +60,11 @@ export default defineComponent({ () => getBreadcrumb() ); - const pathCompile = (path: string): string | Object => { - const { params } = route; - var toPath = pathToRegexp.compile(path); - return toPath(params); - }; + // const pathCompile = (path: string): string | Object => { + // const { params } = route; + // var toPath = pathToRegexp.compile(path); + // return toPath(params); + // }; const handleLink = (item: RouteLocationMatched): any => { const { redirect, path } = item; @@ -72,7 +72,7 @@ export default defineComponent({ router.push(redirect.toString()); return; } - router.push(pathCompile(path)); + router.push(path); }; return { levelList, handleLink }; diff --git a/src/layout/components/AppMain.vue b/src/layout/components/AppMain.vue index 518e56ed1..255481e67 100644 --- a/src/layout/components/AppMain.vue +++ b/src/layout/components/AppMain.vue @@ -13,7 +13,6 @@ diff --git a/src/layout/components/tag/tagsHook.ts b/src/layout/components/tag/tagsHook.ts new file mode 100644 index 000000000..9be9bc71a --- /dev/null +++ b/src/layout/components/tag/tagsHook.ts @@ -0,0 +1,74 @@ +import { reactive, toRefs, nextTick } from "vue" +import { useRouter } from "vue-router" + +interface InterDynamic { + dRoutes: object[], + [propName: string]: any +} + +// 默认显示首页tag +let dynamic: InterDynamic = reactive({ + dRoutes: [ + { + path: "/welcome", meta: { + title: "home", + icon: 'el-icon-s-home', + showLink: true, + savedPosition: false, + } + }] +}) + +export function useDynamicRoutesHook() { + const router = useRouter() + /** + * @param value string 当前menu对应的路由path + * @param parentPath string 当前路由中父级路由 + */ + function dynamicRouteTags(value: string, parentPath: string): void { + const hasValue = dynamic.dRoutes.some((item: any) => { + return item.path === value + }) + function concatPath(arr: object[], value: string, parentPath: string) { + if (!hasValue) { + arr.forEach((arrItem: any) => { + let pathConcat = parentPath + '/' + arrItem.path + if (arrItem.path === value || pathConcat === value) { + dynamic.dRoutes.push({ path: value, meta: arrItem.meta }) + console.log(dynamic.dRoutes) + } else { + if (arrItem.children && arrItem.children.length > 0) { + concatPath(arrItem.children, value, parentPath) + } + } + }) + } + } + concatPath(router.options.routes, value, parentPath) + } + /** + * @param value any 当前删除tag路由 + * @param current objct 当前激活路由对象 + */ + const deleteDynamicTag = async (obj: any, current: object): Promise => { + let valueIndex: number = dynamic.dRoutes.findIndex((item: any) => { + return item.path === obj.path + }) + // 从当前匹配到的路径中删除 + await dynamic.dRoutes.splice(valueIndex, 1) + if (current === obj.path) { // 如果删除当前激活tag就自动切换到最后一个tag + let newRoute: any = dynamic.dRoutes.slice(-1) + nextTick(() => { + router.push({ + path: newRoute[0].path + }) + }) + } + } + + return { + ...toRefs(dynamic), + dynamicRouteTags, + deleteDynamicTag + } +} diff --git a/src/layout/index.vue b/src/layout/index.vue index 428410232..ddc3660bf 100644 --- a/src/layout/index.vue +++ b/src/layout/index.vue @@ -11,6 +11,8 @@
+ +
@@ -21,7 +23,7 @@