feat: ✨ (store)增加了对未定义组件的处理,添加更新页签名称方法
- 在多级菜单详情示例中使用了新的标签名称
This commit is contained in:
@@ -27,8 +27,6 @@ export const usePermissionStore = defineStore("permission", () => {
|
|||||||
|
|
||||||
isDynamicRoutesGenerated.value = true;
|
isDynamicRoutesGenerated.value = true;
|
||||||
|
|
||||||
console.log("dynamicRoutes", dynamicRoutes);
|
|
||||||
|
|
||||||
return dynamicRoutes;
|
return dynamicRoutes;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("❌ Failed to generate routes:", error);
|
console.error("❌ Failed to generate routes:", error);
|
||||||
@@ -85,13 +83,17 @@ const parseDynamicRoutes = (rawRoutes: RouteVO[]): RouteRecordRaw[] => {
|
|||||||
rawRoutes.forEach((route) => {
|
rawRoutes.forEach((route) => {
|
||||||
const normalizedRoute = { ...route } as RouteRecordRaw;
|
const normalizedRoute = { ...route } as RouteRecordRaw;
|
||||||
|
|
||||||
// console.log();
|
if (!normalizedRoute.component) {
|
||||||
|
// 如果没有组件,则将组件设置为 undefined 防止404 例如(多级菜单的父菜单)
|
||||||
// 处理组件路径
|
normalizedRoute.component = undefined;
|
||||||
normalizedRoute.component =
|
} else {
|
||||||
normalizedRoute.component?.toString() === "Layout"
|
// 处理组件路径
|
||||||
? Layout
|
normalizedRoute.component =
|
||||||
: modules[`../../views/${normalizedRoute.component}.vue`];
|
normalizedRoute.component?.toString() === "Layout"
|
||||||
|
? Layout
|
||||||
|
: modules[`../../views/${normalizedRoute.component}.vue`] ||
|
||||||
|
modules[`../../views/error/404.vue`]; // 找不到页面时,返回404页面
|
||||||
|
}
|
||||||
|
|
||||||
// 递归解析子路由
|
// 递归解析子路由
|
||||||
if (normalizedRoute.children) {
|
if (normalizedRoute.children) {
|
||||||
|
|||||||
@@ -97,6 +97,19 @@ export const useTagsViewStore = defineStore("tagsView", () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据路径更新标签名称
|
||||||
|
* @param fullPath 路径
|
||||||
|
* @param title 标签名称
|
||||||
|
*/
|
||||||
|
function updateTagName(fullPath: string, title: string) {
|
||||||
|
const tag = visitedViews.value.find((tag: TagView) => tag.fullPath === fullPath);
|
||||||
|
|
||||||
|
if (tag) {
|
||||||
|
tag.title = title;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function addView(view: TagView) {
|
function addView(view: TagView) {
|
||||||
addVisitedView(view);
|
addVisitedView(view);
|
||||||
addCachedView(view);
|
addCachedView(view);
|
||||||
@@ -257,5 +270,6 @@ export const useTagsViewStore = defineStore("tagsView", () => {
|
|||||||
closeCurrentView,
|
closeCurrentView,
|
||||||
isActive,
|
isActive,
|
||||||
toLastView,
|
toLastView,
|
||||||
|
updateTagName,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,15 +1,21 @@
|
|||||||
import router from "@/router";
|
import router from "@/router";
|
||||||
import { ElButton } from "element-plus";
|
import { ElButton } from "element-plus";
|
||||||
|
import { useTagsViewStore } from "@/store";
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name: "ToDetail",
|
name: "ToDetail",
|
||||||
setup() {
|
setup() {
|
||||||
|
const route = useRoute();
|
||||||
|
const tagsViewStore = useTagsViewStore();
|
||||||
|
|
||||||
// 跳转详情
|
// 跳转详情
|
||||||
const navigateToDetail = async (id: number) => {
|
const navigateToDetail = async (id: number) => {
|
||||||
await router.push({
|
await router.push({
|
||||||
path: "/detail/" + id,
|
path: "/detail/" + id,
|
||||||
query: { message: `msg${id}` },
|
query: { message: `msg${id}` },
|
||||||
});
|
});
|
||||||
|
// 更改标题
|
||||||
|
tagsViewStore.updateTagName(route.fullPath, `详情页缓存(id=${id})`);
|
||||||
};
|
};
|
||||||
return () =>
|
return () =>
|
||||||
h("div", null, [
|
h("div", null, [
|
||||||
|
|||||||
Reference in New Issue
Block a user