vue3动态路由addRoute

需要挂载的路由
const path = [
        {
          path: "/comprehensive2",
          name: "comprehensive2",
          component: "login/login",
          icon: "robot",
          meta: { name: "页面123123", parent: "home" },
          children: [
            {
              path: "/comprehensive4",
              name: "comprehensive4",
              component:"login/login",
              icon: "robot",
              meta: { name: "页面411", parent: "comprehensive2" },
              children: [],
            },
          ],
        },
        {
          path: "/comprehensive34",
          name: "comprehensive34",
          component: "welcome/index",
          icon: "robot",
          meta: { name: "页面1", parent: "home" },
          children: [],
        },
      ];
      const paths = JSON.parse(JSON.stringify(path))
      store.SET_ROUTERS(path); //将路由存储方便路由守卫调用
      SET_ROUTER(paths); //调用添加路由方法
添加路由的方法 addRouter.js
import router from "../../router/index";
const modules = import.meta.glob("@/views/**/*.vue");//获取views下所有文件夹

let newRouter = [];
function dynamicRouter(routers, y) {
  newRouter = router.getRoutes()[3].children;//初始路由
  const list = [];  //最后整理好路由返回
  routers.forEach((itemRouter, index) => {
    // 从本地找组件
    const authRoute = router
      .getRoutes()
      .findIndex((i) => i.path == itemRouter.path); //找到了就代表路由数组已经存在该路由
    const authRoutes = newRouter.findIndex((i) => i.path == itemRouter.path); //找到了就代表路由数组已经存在该路由
    if (authRoute == -1 && authRoutes == -1) {
      itemRouter.component = modules[`/src/views/${itemRouter.component}.vue`] ,//匹配
      list.push(itemRouter); 
      if (y !== 1) {
        newRouter.push(itemRouter);//初始路由添加新路由
      }
      // 是否存在子集
      if (itemRouter.children && itemRouter.children.length) {
        list[index].children = dynamicRouter(itemRouter.children, 1);
      }
    }
  });
  return list;
}
export function SET_ROUTER(val) {
  dynamicRouter(val).forEach((itemRouter) => {
    router.addRoute("home", itemRouter); //父级name名称
  });
}

pinia存储方法(不会pinia的看我另一个文档)
    SET_ROUTERS(val) {
      this.router = val;
    },
promission.js路由守卫文件
import router from "./index.js";
import { useStore } from "@/store/index";
import { createRouter, createWebHashHistory } from "vue-router";
import { SET_ROUTER } from "@/views/login/addRouter"; //addRoute方法文件

let registerRouteFresh = true; //防止多次调用添加路由
router.beforeEach((to, from, next) => {
  // NProgress.start();
  window.document.title = to.meta.name;
  if (to.path == "/login") {
    const store = useStore();
    store.$reset(); //重置存储器
    next();
  } else {
    if (registerRouteFresh) {
      const store = useStore();
      SET_ROUTER(JSON.parse(JSON.stringify(store.router)));
      registerRouteFresh = false;
      next({path:`${to.path}`}) //重定向锁定路由
    } else {
      next();
    }
  }

  // NProgress.done();
});
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue3中,可以使用router.addRoute()方法来实现动态路由的添加。这个方法可以接受两个参数,第一个参数可以是父级路由对象或者父级路由的路径,第二个参数是要添加的路由对象。通过调用这个方法,可以动态地向路由中添加单个路由。\[1\] 以下是一个示例代码,展示了如何在Vue3中使用addRoute()方法实现动态路由的添加: ```javascript import { createRouter } from 'vue-router' const router = createRouter({ // 路由配置 }) // 添加单个路由 const route = { path: '/dynamic', name: 'Dynamic', component: () => import('@/views/Dynamic.vue') } router.addRoute(route) export default router ``` 在上面的代码中,我们首先创建了一个路由实例,然后定义了一个要添加的路由对象。最后,通过调用addRoute()方法将这个路由对象添加到路由中。 需要注意的是,在Vue3中,使用addRoute()方法添加的路由会立即生效,无需重新创建路由实例或调用其他方法。这样,新添加的路由就可以在页面上进行渲染了。 希望这个回答对你有帮助! #### 引用[.reference_title] - *1* *3* [vue3动态路由addRoute](https://blog.csdn.net/m0_49343686/article/details/123959075)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue3 使用addRoute动态添加路由,兼解决刷新空白或跳到404页面问题](https://blog.csdn.net/YSQ_qsy/article/details/122042229)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值