明树Git Lab

Commit 6c6712a9 authored by zhanghan's avatar zhanghan

历史路由处理

parent b7568991
Pipeline #108701 passed with stage
in 20 seconds
...@@ -63,9 +63,9 @@ ...@@ -63,9 +63,9 @@
> >
<el-tab-pane <el-tab-pane
v-for="view in visitedViews" v-for="view in visitedViews"
:key="view.path" :key="view.fullPath"
:label="view.title" :label="view.title"
:name="view.path" :name="view.fullPath"
/> />
</el-tabs> </el-tabs>
</div> </div>
...@@ -114,56 +114,84 @@ const router = useRouter(); ...@@ -114,56 +114,84 @@ const router = useRouter();
const route = useRoute(); const route = useRoute();
// ========== 路由标签页相关 ========== // ========== 路由标签页相关 ==========
const visitedViews = ref([]); // 已访问的路由列表 const visitedViews = ref([]);
const activePath = ref(""); // 当前激活的路由路径 const activePath = ref("");
// 添加路由到已访问列表
// 添加路由到已访问列表(合并同路径不同参数的路由)
const addView = (view) => { const addView = (view) => {
// 检查路由是否已存在 const basePath = view.path;
const index = visitedViews.value.findIndex((v) => v.path === view.path); const fullPath = view.fullPath || view.path;
if (index === -1) {
// 不存在则添加,优先使用 title,其次 meta.title,最后 name // 1. 先找同基础路径的路由
const samePathIndex = visitedViews.value.findIndex(
(v) => v.path === basePath,
);
if (samePathIndex !== -1) {
// 2. 如果存在同路径路由,更新它的参数和fullPath(不新增)
visitedViews.value[samePathIndex] = {
...visitedViews.value[samePathIndex],
fullPath: fullPath,
query: { ...view.query },
params: { ...view.params },
};
} else {
// 3. 不存在则新增
visitedViews.value.push({ visitedViews.value.push({
path: view.path, path: basePath,
fullPath: fullPath,
query: { ...view.query },
params: { ...view.params },
title: view.title || view.meta?.title || view.name || "未命名", title: view.title || view.meta?.title || view.name || "未命名",
}); });
console.log(visitedViews, "visitedViewsvisitedViews");
} }
// 设置当前激活的路由
activePath.value = view.path; // 更新激活状态
activePath.value = fullPath;
}; };
// 点击标签切换路由 // 点击标签切换路由(优化:确保切换时参数正确)
const handleTabClick = (tab) => { const handleTabClick = (tab) => {
const path = tab.paneName; const fullPath = tab.paneName;
if (path !== route.path) { if (fullPath !== route.fullPath) {
router.push(path); const targetView = visitedViews.value.find((v) => v.fullPath === fullPath);
if (targetView) {
router.push({
path: targetView.path,
query: targetView.query,
params: targetView.params,
});
} else {
// 备用方案:直接通过fullPath跳转
router.push(fullPath);
}
} }
}; };
// 关闭标签 // 关闭标签(逻辑调整:删除同路径的所有记录)
const handleTabRemove = (targetPath) => { const handleTabRemove = (targetFullPath) => {
const index = visitedViews.value.findIndex((v) => v.path === targetPath); const targetView = visitedViews.value.find(
if (index !== -1) { (v) => v.fullPath === targetFullPath,
// 移除路由 );
visitedViews.value.splice(index, 1); if (!targetView) return;
// 如果关闭的是当前路由,则跳转到上一个路由 // 根据基础路径删除(确保同路径的标签只删一次)
if (targetPath === route.path) { const index = visitedViews.value.findIndex((v) => v.path === targetView.path);
if (index !== -1) {
// 跳转逻辑保持不变
if (targetFullPath === route.fullPath) {
if (visitedViews.value.length > 0) { if (visitedViews.value.length > 0) {
// 跳转到相邻路由(优先后面的,如果没有则前面的)
const nextView = const nextView =
visitedViews.value[index] || visitedViews.value[index - 1]; visitedViews.value[index] || visitedViews.value[index - 1];
if (nextView) { if (nextView) {
router.push(nextView.path); router.push(nextView.fullPath);
} }
} else { } else {
// 如果没有其他路由,跳转到首页
router.push("/"); router.push("/");
} }
} }
} }
}; };
// ========== 路由标签页相关结束 ========== // ========== 路由标签页相关结束 ==========
// 获取资源库数据 // 获取资源库数据
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment