Skip to content

Commit 7c1d5f6

Browse files
committed
feat: ✨ (store)增加了对未定义组件的处理,添加更新页签名称方法
- 在多级菜单详情示例中使用了新的标签名称
1 parent 5d3e92b commit 7c1d5f6

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/store/modules/permission-store.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export const usePermissionStore = defineStore("permission", () => {
2727

2828
isDynamicRoutesGenerated.value = true;
2929

30-
console.log("dynamicRoutes", dynamicRoutes);
31-
3230
return dynamicRoutes;
3331
} catch (error) {
3432
console.error("❌ Failed to generate routes:", error);
@@ -85,13 +83,17 @@ const parseDynamicRoutes = (rawRoutes: RouteVO[]): RouteRecordRaw[] => {
8583
rawRoutes.forEach((route) => {
8684
const normalizedRoute = { ...route } as RouteRecordRaw;
8785

88-
// console.log();
89-
90-
// 处理组件路径
91-
normalizedRoute.component =
92-
normalizedRoute.component?.toString() === "Layout"
93-
? Layout
94-
: modules[`../../views/${normalizedRoute.component}.vue`];
86+
if (!normalizedRoute.component) {
87+
// 如果没有组件,则将组件设置为 undefined 防止404 例如(多级菜单的父菜单)
88+
normalizedRoute.component = undefined;
89+
} else {
90+
// 处理组件路径
91+
normalizedRoute.component =
92+
normalizedRoute.component?.toString() === "Layout"
93+
? Layout
94+
: modules[`../../views/${normalizedRoute.component}.vue`] ||
95+
modules[`../../views/error/404.vue`]; // 找不到页面时,返回404页面
96+
}
9597

9698
// 递归解析子路由
9799
if (normalizedRoute.children) {

src/store/modules/tags-view-store.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ export const useTagsViewStore = defineStore("tagsView", () => {
9797
}
9898
}
9999

100+
/**
101+
* 根据路径更新标签名称
102+
* @param fullPath 路径
103+
* @param title 标签名称
104+
*/
105+
function updateTagName(fullPath: string, title: string) {
106+
const tag = visitedViews.value.find((tag: TagView) => tag.fullPath === fullPath);
107+
108+
if (tag) {
109+
tag.title = title;
110+
}
111+
}
112+
100113
function addView(view: TagView) {
101114
addVisitedView(view);
102115
addCachedView(view);
@@ -257,5 +270,6 @@ export const useTagsViewStore = defineStore("tagsView", () => {
257270
closeCurrentView,
258271
isActive,
259272
toLastView,
273+
updateTagName,
260274
};
261275
});

src/views/demo/multi-level/children/children/detail.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import router from "@/router";
22
import { ElButton } from "element-plus";
3+
import { useTagsViewStore } from "@/store";
34

45
export default defineComponent({
56
name: "ToDetail",
67
setup() {
8+
const route = useRoute();
9+
const tagsViewStore = useTagsViewStore();
10+
711
// 跳转详情
812
const navigateToDetail = async (id: number) => {
913
await router.push({
1014
path: "/detail/" + id,
1115
query: { message: `msg${id}` },
1216
});
17+
// 更改标题
18+
tagsViewStore.updateTagName(route.fullPath, `详情页缓存(id=${id})`);
1319
};
1420
return () =>
1521
h("div", null, [

0 commit comments

Comments
 (0)