use-navigation.ts 829 B

123456789101112131415161718192021222324252627282930313233
  1. import { type RouteRecordNormalized, useRouter } from 'vue-router';
  2. import { isHttpUrl, openRouteInNewWindow, openWindow } from '@vben/utils';
  3. function useNavigation() {
  4. const router = useRouter();
  5. const routes = router.getRoutes();
  6. const routeMetaMap = new Map<string, RouteRecordNormalized>();
  7. routes.forEach((route) => {
  8. routeMetaMap.set(route.path, route);
  9. });
  10. const navigation = async (path: string) => {
  11. const route = routeMetaMap.get(path);
  12. const { openInNewWindow = false, query = {} } = route?.meta ?? {};
  13. if (isHttpUrl(path)) {
  14. openWindow(path, { target: '_blank' });
  15. } else if (openInNewWindow) {
  16. openRouteInNewWindow(path);
  17. } else {
  18. await router.push({
  19. path,
  20. query,
  21. });
  22. }
  23. };
  24. return { navigation };
  25. }
  26. export { useNavigation };