|
@@ -1,9 +1,10 @@
|
|
<script setup lang="ts">
|
|
<script setup lang="ts">
|
|
-import { useRouter } from "vue-router";
|
|
|
|
|
|
+import { RouteRecordRaw, useRouter } from "vue-router";
|
|
import path from "path-browserify";
|
|
import path from "path-browserify";
|
|
import { isExternal } from "@/utils/index";
|
|
import { isExternal } from "@/utils/index";
|
|
import AppLink from "./Link.vue";
|
|
import AppLink from "./Link.vue";
|
|
import SvgIcon from "@/components/SvgIcon/index.vue";
|
|
import SvgIcon from "@/components/SvgIcon/index.vue";
|
|
|
|
+import { SidebarRoutes } from "@/layout/components/Sidebar/types";
|
|
|
|
|
|
const router = useRouter();
|
|
const router = useRouter();
|
|
const props = defineProps({
|
|
const props = defineProps({
|
|
@@ -22,7 +23,8 @@ const props = defineProps({
|
|
required: true,
|
|
required: true,
|
|
},
|
|
},
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+//const {item} = props;
|
|
|
|
+const itemRoute: SidebarRoutes = reactive(props.item);
|
|
const onlyOneChild = ref(); // 临时变量,唯一子路由
|
|
const onlyOneChild = ref(); // 临时变量,唯一子路由
|
|
/**
|
|
/**
|
|
* 判断当前路由是否只有一个子路由
|
|
* 判断当前路由是否只有一个子路由
|
|
@@ -33,9 +35,9 @@ const onlyOneChild = ref(); // 临时变量,唯一子路由
|
|
* @param children 子路由数组
|
|
* @param children 子路由数组
|
|
* @param parent 当前路由
|
|
* @param parent 当前路由
|
|
*/
|
|
*/
|
|
-function hasOneShowingChild(children = [], parent: any) {
|
|
|
|
|
|
+function hasOneShowingChild(children: any = [], parent: any) {
|
|
// 需要显示的子路由数组
|
|
// 需要显示的子路由数组
|
|
- const showingChildren = children.filter((item: any) => {
|
|
|
|
|
|
+ const showingChildren: any = children.filter((item: any) => {
|
|
if (item.meta?.hidden) {
|
|
if (item.meta?.hidden) {
|
|
return false; // 过滤不显示的子路由
|
|
return false; // 过滤不显示的子路由
|
|
} else {
|
|
} else {
|
|
@@ -66,25 +68,27 @@ function hasOneShowingChild(children = [], parent: any) {
|
|
*
|
|
*
|
|
* @param routePath 路由路径
|
|
* @param routePath 路由路径
|
|
*/
|
|
*/
|
|
-function resolvePath(routePath: string) {
|
|
|
|
|
|
+function resolvePath(routePath: string): string {
|
|
if (isExternal(routePath)) {
|
|
if (isExternal(routePath)) {
|
|
return routePath;
|
|
return routePath;
|
|
}
|
|
}
|
|
- if (isExternal(props.basePath)) {
|
|
|
|
- return props.basePath;
|
|
|
|
|
|
+ if (isExternal(props.basePath || "")) {
|
|
|
|
+ return props.basePath || "";
|
|
}
|
|
}
|
|
// 完整路径 = 父级路径(/level/level_3) + 路由路径
|
|
// 完整路径 = 父级路径(/level/level_3) + 路由路径
|
|
- const fullPath = path.resolve(props.basePath, routePath); // 相对路径 → 绝对路径
|
|
|
|
- return fullPath;
|
|
|
|
|
|
+ // return fullPath 相对路径 → 绝对路径
|
|
|
|
+ return path.resolve(props.basePath || "", routePath);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 处理子页面时父级菜单高亮
|
|
* 处理子页面时父级菜单高亮
|
|
*/
|
|
*/
|
|
-function addActiveClass(routePath) {
|
|
|
|
- const currentBase = routePath.split("/")[1];
|
|
|
|
|
|
+function addActiveClass(routePath: string) {
|
|
|
|
+ const currentBase: string = routePath.split("/")[1];
|
|
document.querySelectorAll(".el-menu-item").forEach((item) => {
|
|
document.querySelectorAll(".el-menu-item").forEach((item) => {
|
|
- const pathBase = item.attributes["mack"].value.split("/")[1];
|
|
|
|
|
|
+ const pathBase: string = item.attributes
|
|
|
|
+ .getNamedItem("mark")
|
|
|
|
+ .value.split("/")[1];
|
|
if (currentBase == pathBase) {
|
|
if (currentBase == pathBase) {
|
|
const active = document.querySelectorAll(".el-menu-item.is-active");
|
|
const active = document.querySelectorAll(".el-menu-item.is-active");
|
|
if (active && active.length) {
|
|
if (active && active.length) {
|
|
@@ -115,10 +119,10 @@ if (onMounted) {
|
|
}
|
|
}
|
|
</script>
|
|
</script>
|
|
<template>
|
|
<template>
|
|
- <div v-if="!item.meta || !item.meta.hidden">
|
|
|
|
|
|
+ <div v-if="!itemRoute.meta || !itemRoute.meta.hidden">
|
|
<template
|
|
<template
|
|
v-if="
|
|
v-if="
|
|
- hasOneShowingChild(item.children, item) &&
|
|
|
|
|
|
+ hasOneShowingChild(itemRoute.children, itemRoute) &&
|
|
(!onlyOneChild.children || onlyOneChild.noShowingChildren)
|
|
(!onlyOneChild.children || onlyOneChild.noShowingChildren)
|
|
"
|
|
"
|
|
>
|
|
>
|
|
@@ -126,7 +130,7 @@ if (onMounted) {
|
|
<app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
|
|
<app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
|
|
<el-menu-item
|
|
<el-menu-item
|
|
:index="resolvePath(onlyOneChild.path)"
|
|
:index="resolvePath(onlyOneChild.path)"
|
|
- :mack="resolvePath(onlyOneChild.path)"
|
|
|
|
|
|
+ :mark="resolvePath(onlyOneChild.path)"
|
|
>
|
|
>
|
|
<svg-icon
|
|
<svg-icon
|
|
v-if="onlyOneChild.meta && onlyOneChild.meta.icon"
|
|
v-if="onlyOneChild.meta && onlyOneChild.meta.icon"
|
|
@@ -138,17 +142,19 @@ if (onMounted) {
|
|
</el-menu-item>
|
|
</el-menu-item>
|
|
</app-link>
|
|
</app-link>
|
|
</template>
|
|
</template>
|
|
- <el-sub-menu v-else :index="resolvePath(item.path)" teleported>
|
|
|
|
|
|
+ <el-sub-menu v-else :index="resolvePath(itemRoute.path)" teleported>
|
|
<!-- 包含多个子路由 $route.path -->
|
|
<!-- 包含多个子路由 $route.path -->
|
|
<template #title>
|
|
<template #title>
|
|
<svg-icon
|
|
<svg-icon
|
|
- v-if="item.meta && item.meta.icon"
|
|
|
|
- :icon-class="item.meta.icon"
|
|
|
|
|
|
+ v-if="itemRoute.meta && itemRoute.meta.icon"
|
|
|
|
+ :icon-class="itemRoute.meta.icon"
|
|
/>
|
|
/>
|
|
- <span v-if="item.meta && item.meta.title">{{ item.meta.title }}</span>
|
|
|
|
|
|
+ <span v-if="itemRoute.meta && itemRoute.meta.title">{{
|
|
|
|
+ itemRoute.meta.title
|
|
|
|
+ }}</span>
|
|
</template>
|
|
</template>
|
|
<sidebar-item
|
|
<sidebar-item
|
|
- v-for="child in item.children"
|
|
|
|
|
|
+ v-for="child in itemRoute.children"
|
|
:key="child.path"
|
|
:key="child.path"
|
|
:item="child"
|
|
:item="child"
|
|
:base-path="resolvePath(child.path)"
|
|
:base-path="resolvePath(child.path)"
|