fix: 🐛 修复混合布局路由参数重定向丢失问题

This commit is contained in:
ray
2024-10-10 00:58:15 +08:00
parent f3bc98e2d3
commit 657564bff3

View File

@@ -1,4 +1,4 @@
<!-- 混合布局顶部 --> <!-- 混合布局顶部菜单 -->
<template> <template>
<el-scrollbar> <el-scrollbar>
<el-menu <el-menu
@@ -10,7 +10,7 @@
@select="handleMenuSelect" @select="handleMenuSelect"
> >
<el-menu-item <el-menu-item
v-for="route in mixTopMenus" v-for="route in topMenus"
:key="route.path" :key="route.path"
:index="route.path" :index="route.path"
> >
@@ -24,13 +24,10 @@
</el-icon> </el-icon>
<svg-icon v-else :icon-class="route.meta.icon" /> <svg-icon v-else :icon-class="route.meta.icon" />
</template> </template>
<span v-if="route.path === '/'">首页</span> <span v-if="route.path === '/'">首页</span>
<template v-else> <span v-else-if="route.meta && route.meta.title" class="ml-1">
<span v-if="route.meta && route.meta.title" class="ml-1"> {{ translateRouteTitle(route.meta.title) }}
{{ translateRouteTitle(route.meta.title) }} </span>
</span>
</template>
</template> </template>
</el-menu-item> </el-menu-item>
</el-menu> </el-menu>
@@ -38,61 +35,71 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
/**
* 导入模块:先外部库,再内部模块,最后导入样式和工具类
*/
import { LocationQueryRaw, RouteRecordRaw } from "vue-router";
import { usePermissionStore, useAppStore } from "@/store"; import { usePermissionStore, useAppStore } from "@/store";
import { translateRouteTitle } from "@/utils/i18n"; import { translateRouteTitle } from "@/utils/i18n";
import variables from "@/styles/variables.module.scss"; import variables from "@/styles/variables.module.scss";
import { RouteRecordRaw } from "vue-router";
/**
* 定义状态:先定义 reactive、ref 或 computed 状态
*/
const router = useRouter();
const appStore = useAppStore(); const appStore = useAppStore();
const permissionStore = usePermissionStore(); const permissionStore = usePermissionStore();
const router = useRouter();
// 避免 activeTopMenuPath 缓存被清理,从当前路由路径获取顶部菜单路径 // 当前激活的顶部菜单路径
// eg. / system / user → /system / dashboard → / const activePath = computed(() => appStore.activeTopMenuPath);
// 顶部菜单列表
const topMenus = ref<RouteRecordRaw[]>([]);
// 获取当前路由路径的顶部菜单路径
const activeTopMenuPath = const activeTopMenuPath =
useRoute().path.split("/").filter(Boolean).length > 1 useRoute().path.split("/").filter(Boolean).length > 1
? useRoute().path.match(/^\/[^\/]+/)?.[0] || "/" ? useRoute().path.match(/^\/[^\/]+/)?.[0] || "/"
: "/"; : "/";
// 设置当前激活的顶部菜单路径
appStore.activeTopMenu(activeTopMenuPath); appStore.activeTopMenu(activeTopMenuPath);
// 激活的顶部菜单路径
const activePath = computed(() => appStore.activeTopMenuPath);
// 混合模式顶部菜单集合
const mixTopMenus = ref<RouteRecordRaw[]>([]);
/** /**
* 菜单选择事件 * 处理菜单点击事件,切换顶部菜单并加载对应的左侧菜单
* @param routePath 点击的菜单路径
*/ */
const handleMenuSelect = (routePath: string) => { const handleMenuSelect = (routePath: string) => {
appStore.activeTopMenu(routePath); appStore.activeTopMenu(routePath); // 设置激活的顶部菜单
permissionStore.setMixLeftMenus(routePath); permissionStore.setMixLeftMenus(routePath); // 更新左侧菜单
// 获取左侧菜单集合,默认跳转到第一个菜单 navigateToFirstLeftMenu(permissionStore.mixLeftMenus); // 跳转到左侧第一个菜单
const mixLeftMenus = permissionStore.mixLeftMenus;
goToFirstMenu(mixLeftMenus);
}; };
/** /**
* 默认跳转到左侧第一个菜单 * 跳转到左侧第一个可访问的菜单
* @param menus 左侧菜单列表
*/ */
const goToFirstMenu = (menus: RouteRecordRaw[]) => { const navigateToFirstLeftMenu = (menus: RouteRecordRaw[]) => {
if (menus.length === 0) return; if (menus.length === 0) return;
const [first] = menus; const [firstMenu] = menus;
if (first.children && first.children.length > 0) { // 如果第一个菜单有子菜单,递归跳转到第一个子菜单
goToFirstMenu(first.children as RouteRecordRaw[]); if (firstMenu.children && firstMenu.children.length > 0) {
} else if (first.name) { navigateToFirstLeftMenu(firstMenu.children as RouteRecordRaw[]);
} else if (firstMenu.name) {
router.push({ router.push({
name: first.name, name: firstMenu.name,
query:
typeof firstMenu.meta?.params === "object"
? (firstMenu.meta.params as LocationQueryRaw)
: undefined,
}); });
} }
}; };
// 初始化顶部菜单
onMounted(() => { onMounted(() => {
mixTopMenus.value = permissionStore.routes.filter( topMenus.value = permissionStore.routes.filter(
(item) => !item.meta || !item.meta.hidden (item) => !item.meta || !item.meta.hidden
); );
}); });