• Vue添加动态路由后,不同角色访问“/”产生404问题


    场景

    系统基于 若依 二次开发;动态的加载路由已经集成好了;

    根据业务需要,不同角色访问的首页是不同的,一顿操作后,每次访问 / 时一直会进入404页面内。


    原本的代码

    permission.js

    1. router.beforeEach((to, from, next) => {
    2. if (getToken()) {
    3. /* has token*/
    4. if (to.path === "/login") {
    5. next({ path: "/" });
    6. } else {
    7. if (store.getters.roles.length === 0) {
    8. isRelogin.show = true;
    9. // 判断当前用户是否已拉取完user_info信息
    10. store
    11. .dispatch("GetInfo")
    12. .then((res) => {
    13. isRelogin.show = false;
    14. store.dispatch("GenerateRoutes").then(accessRoutes => {
    15. router.addRoutes(accessRoutes); // 动态添加可访问路由表
    16. next({ ...to, replace: true });
    17. });
    18. })
    19. .catch(err => {
    20. store.dispatch("LogOut").then(() => {
    21. next({ path: "/" });
    22. });
    23. });
    24. } else {
    25. next();
    26. }
    27. }
    28. } else {
    29. // 没有token
    30. if (whiteList.indexOf(to.path) !== -1) {
    31. next();
    32. } else {
    33. next(`/login?redirect=${to.fullPath}`); // 否则全部重定向到登录页
    34. }
    35. }
    36. });

    router.js 

    1. // 公共路由
    2. export const constantRoutes = [
    3. {
    4. path: "/redirect",
    5. component: Layout,
    6. hidden: true,
    7. children: [
    8. {
    9. path: "/redirect/:path(.*)",
    10. component: () => import("@/views/redirect"),
    11. },
    12. ],
    13. },
    14. {
    15. path: "/login",
    16. component: () => import("@/views/login"),
    17. hidden: true,
    18. },
    19. {
    20. path: "/404",
    21. component: () => import("@/views/error/404"),
    22. hidden: true,
    23. },
    24. {
    25. path: "/401",
    26. component: () => import("@/views/error/401"),
    27. hidden: true,
    28. }
    29. ];
    30. export default new Router({
    31. mode: "history",
    32. scrollBehavior: () => ({ y: 0 }),
    33. routes: [...constantRoutes],
    34. });

    accessRoutes: accessRoutes是经过store中处理之后的,最终结构如下

    1. [
    2. {
    3. path: "/xx/xx",
    4. component: Layout,
    5. children: [{}, ...],
    6. },
    7. ...多个路由,
    8. {
    9. path: "*",
    10. redirect: '/404',
    11. hidden: true
    12. }
    13. ];

    因为业务需要,不同的角色访问地址 / 时,跳转的页面不同。以及一些其他原因。所以需要对accessRoutes进行处理;

    修改之后的代码

    permission.js

    1. store.dispatch("GenerateRoutes").then(accessRoutes => {
    2. const mainRoute = {
    3. path: "/",
    4. component: () => import("@/layout/main.vue"),
    5. redirect: getFirstPath("", accessRoutes),
    6. children: [...accessRoutes],
    7. };
    8. router.addRoutes([mainRoute]);
    9. next({ ...to, replace: true });
    10. });

    但是最终运行结果是,地址栏访问动态的路由 /xx/xx 时可以访问到;但是访问 / 时却一直会跳转到404页面中; 


    原因

    网上查博客,查到一种方法可行,恰巧也是使用的若依;

    RuoYi-Vue——关于登录后不同角色跳不同页面

    该方法是在 router.addRoutes 之后对角色进行判断,然后跳转不同的相应的页面。

    🙄But,问题解决了,但是我根据角色对"/"路由设置不同的 redirect 为什么会一直404呢?

    网上查不到,只能自己调试代码了。。。

    1. 404原因?

    先在router.addRoute之后的next行调试,这个时候动态路由已经添加进去了。

        此时,to.path == "/"。一直向下到VueRouter中match匹配路由的地方,在pathList中发现通配符 * 的路由在 "" 之前。

    path: "/" 或 path: "" 的路由,vueRouter会在内部转换成 ""

     所以,每次访问 "/"的时候,会一直提示404;

     2. 通配符 * 在 "" 之前?

    对router.addRoute进行调试,一直向下找到addRouteRecord方法,你会发现,vueRouter添加router record的时候,对新进来的routes数组是以深度优先进行遍历的。

    而代码中的 addRoute 添加的路由结构是:

    1. [
    2. {
    3. path: "/",
    4. redirect: "xxx",
    5. children: [..., ..., { path: "*", redirect: "/404" }]
    6. }
    7. ]

     所以,pathList中的的 "*" 会在 "" 之前,就会导致每次访问“/”的时候一直跳转到404页面;

     问题原因找到了,所以只需要对通配符的路由提出来,放到最后就可以完美解决问题了!

    🤣

    router.addRoutes([mainRoute, { path: '*', redirect: '/404', hidden: true }]); 
  • 相关阅读:
    阿里云RDS关系型数据库详细介绍_多版本数据库说明
    【数据结构】优先级队列
    从Github中下载部分文件
    【C++】lock_guard用法
    基于chatgpt的聊天机器人
    按照功能对Boost库进行分类
    E Parity Split (江西CCPC省赛)题解
    Windows 的 docker 删除容器后 WSL2 磁盘空间不释放的问题
    HTTP中的GET方法与POST方法
    Elasticsearch-02-es的restapi使用
  • 原文地址:https://blog.csdn.net/qq_36157085/article/details/126497956