页面路径完善
This commit is contained in:
@@ -19,57 +19,41 @@ const permissionStore = usePermissionStore();
|
||||
const levelList = ref<RouteLocationMatched[]>([]);
|
||||
|
||||
const getBreadcrumb = () => {
|
||||
// only show routes with meta.title
|
||||
let matched = [];
|
||||
const pathNum = findPathNum(route.path);
|
||||
// multi-level menu
|
||||
if (pathNum > 2) {
|
||||
const reg = /\/\w+/gi;
|
||||
const pathList = route.path.match(reg).map((item, index) => {
|
||||
if (index !== 0) item = item.slice(1);
|
||||
return item;
|
||||
});
|
||||
getMatched(pathList, permissionStore.defaultRoutes, matched);
|
||||
} else {
|
||||
matched = route.matched.filter((item) => item.meta && item.meta.title);
|
||||
}
|
||||
// 判断是否为首页
|
||||
if (!isDashboard(matched[0])) {
|
||||
matched = [{ path: '/index', meta: { title: '首页' } }].concat(matched);
|
||||
}
|
||||
levelList.value = matched.filter((item) => item.meta && item.meta.title && item.meta.breadcrumb !== false);
|
||||
};
|
||||
const findPathNum = (str, char = '/') => {
|
||||
if (typeof str !== 'string' || str.length === 0) return 0;
|
||||
return str.split(char).length - 1;
|
||||
};
|
||||
const getMatched = (pathList, routeList, matched) => {
|
||||
const data = routeList.find((item) => item.path == pathList[0] || (item.name += '').toLowerCase() == pathList[0]);
|
||||
if (data) {
|
||||
matched.push(data);
|
||||
if (data.children && pathList.length) {
|
||||
pathList.shift();
|
||||
getMatched(pathList, data.children, matched);
|
||||
// 1. 先获取原生的 matched
|
||||
let matched = route.matched.filter((item) => item.meta && item.meta.title);
|
||||
|
||||
// 🔥 关键:如果当前路由有 parentTitle,手动插入虚拟父级
|
||||
if (route.meta?.parentTitle) {
|
||||
// 找到倒数第二个路由(通常是二级列表页)
|
||||
const lastIndex = matched.length - 1;
|
||||
if (lastIndex >= 0) {
|
||||
// 创建虚拟父级路由对象
|
||||
const virtualParent = {
|
||||
path: matched[lastIndex].path,
|
||||
meta: { title: route.meta.parentTitle }
|
||||
};
|
||||
|
||||
// 插入到倒数第二个位置
|
||||
matched = [...matched.slice(0, lastIndex), virtualParent, matched[lastIndex]];
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 过滤掉设置了 breadcrumb: false 的路由
|
||||
levelList.value = matched.filter((item) => item.meta && item.meta.title && item.meta.breadcrumb !== false);
|
||||
|
||||
console.log('最终面包屑:', levelList.value);
|
||||
};
|
||||
const isDashboard = (route: RouteLocationMatched) => {
|
||||
const name = route && (route.name as string);
|
||||
if (!name) {
|
||||
return false;
|
||||
}
|
||||
return name.trim() === 'Index';
|
||||
};
|
||||
|
||||
const handleLink = (item) => {
|
||||
const { redirect, path } = item;
|
||||
redirect ? router.push(redirect) : router.push(path);
|
||||
};
|
||||
|
||||
watchEffect(() => {
|
||||
// if you go to the redirect page, do not update the breadcrumbs
|
||||
if (route.path.startsWith('/redirect/')) return;
|
||||
getBreadcrumb();
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
getBreadcrumb();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user