You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { ref } from 'vue';
  2. import { router } from '@/router';
  3. export const useAsRouter = () => {
  4. // 获取当前路由
  5. function routerCur() {
  6. return router.currentRoute.value.path
  7. }
  8. // 获取当前路由父路由
  9. function routerArrayCur() {
  10. if(router.currentRoute.value.matched.length > 3) {
  11. return [router.currentRoute.value.matched[1].path, router.currentRoute.value.matched[2].path]
  12. } else {
  13. return [router.currentRoute.value.matched[1].path]
  14. }
  15. }
  16. // 跳转路由
  17. function routerTo(url : string) {
  18. router.push(url)
  19. }
  20. // 匹配路由页面
  21. function routerDynamic(originRouter, actionsArray) {
  22. const finalRouter = originRouter.filter(item => actionsArray.some(action => action.action === item.name))
  23. .map((item) => ({
  24. ...item,
  25. title: actionsArray.find(action => action.action === item.name)?.name || item.meta.title,
  26. meta: {
  27. ...item.meta,
  28. title: actionsArray.find(action => action.action === item.name)?.name || item.meta.title,
  29. },
  30. children: item.children ? routerDynamic(item.children, actionsArray) : [],
  31. }));
  32. return finalRouter;
  33. }
  34. // 添加路由
  35. function routerAdd(route) {
  36. router.addRoute('', route);
  37. }
  38. // 初始化路由
  39. function routerInit() {
  40. return router.options.routes + router.options.routes.LayoutRoute;
  41. }
  42. return {
  43. routerCur,
  44. routerArrayCur,
  45. routerTo,
  46. routerDynamic,
  47. routerAdd,
  48. routerInit
  49. }
  50. }