feat: 路由参数

This commit is contained in:
dap
2024-10-10 11:48:26 +08:00
parent fffe2d0db9
commit 60d513ce40
8 changed files with 124 additions and 6 deletions

View File

@@ -10,6 +10,7 @@ import {
generateRoutesByBackend,
generateRoutesByFrontend,
mapTree,
setObjToUrlParams,
} from '@vben/utils';
async function generateAccessible(
@@ -82,7 +83,14 @@ async function generateRoutes(
return route;
}
route.redirect = firstChild.path;
// 第一个路由如果有query参数 需要加上参数
const fistChildQuery = route.children[0]?.meta?.query;
// 根目录菜单固定只有一个children 且path为/ 不需要添加redirect
route.redirect =
fistChildQuery && route.children.length !== 1 && route.path !== '/'
? setObjToUrlParams(firstChild.path, fistChildQuery)
: firstChild.path;
return route;
});

View File

@@ -1,14 +1,21 @@
import { useRouter } from 'vue-router';
import { isHttpUrl, openWindow } from '@vben/utils';
import { isHttpUrl, isObject, openWindow } from '@vben/utils';
function useNavigation() {
const router = useRouter();
const allRoutes = router.getRoutes();
const navigation = async (path: string) => {
if (isHttpUrl(path)) {
openWindow(path, { target: '_blank' });
} else {
// 带路由参数
const found = allRoutes.find((item) => item.path === path);
if (found && isObject(found.meta.query)) {
await router.push({ path, query: found.meta.query });
return;
}
await router.push(path);
}
};