Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多层级路由+重定向配置冗余,实现三级菜单需求 #7830

Closed
rawbin- opened this issue Feb 8, 2022 · 0 comments
Closed

多层级路由+重定向配置冗余,实现三级菜单需求 #7830

rawbin- opened this issue Feb 8, 2022 · 0 comments

Comments

@rawbin-
Copy link

rawbin- commented Feb 8, 2022

需求:
实现一个三级结构,菜单里面只显示两级,面包屑里面显示三级,面包屑可导航(这就涉及到每一级要有内容渲染,第一级需要重定向)
如图所示结果
image

实现过程:
按照预期的结果写了一遍路由结构,差不多这样

{
    path: '/menu1',
    name: '菜单分类一', 
    redirect: '/menu1/sub1',  
    routes: [
      {
        path: '/menu1/sub1',
        name: '菜单分类一功能一列表',
        component: './menu1/menu1sub1/menu1sub1list.tsx',
        routes: [
          {
            path: '/menu1/sub1/detail',
            name: '菜单分类一功能一详情',
            component: './menu1/menu1sub1/menu1sub1listdetail.tsx',
            hideInMenu: true,
          },
        ],
      },
      {
        path: '/menu1/sub2',
        name: '菜单分类一功能二列表',
        component: './menu1/menu1sub1/menu1sub1list.tsx',
        routes: [
          {
            path: '/menu1/sub2/detail',
            name: '菜单分类一功能二详情',
            component: './menu1/menu1sub1/menu1sub1listdetail.tsx',
            hideInMenu: true,
          },
        ],
      },
    ],
  }

中间遇到过几个问题:
1、redirect 导致所有子菜单不渲染
2、非叶子路由(list)上加了component,导致子路由不渲染(detail)

经过多方尝试,然后参考了 https://github.com/umijs/umi/issues/5221,最后得到一个可行的结果:

  {
    path: '/menu1',
    name: '菜单分类一',
    // redirect: '/menu1/sub1', // 加了这个所有子页面都不渲染了
    routes: [
      {
        path: 'menu1', // 这个是用来模拟重定向的
        name: '菜单分类一',
        component: './menu1/menu1sub1/menu1sub1list.tsx',
        hideInMenu: true,
      },
      {
        path: '/menu1/sub1',
        name: '菜单分类一功能一列表',
        routes: [
          {
            path: '/menu1/sub1',
            name: '菜单分类一功能一列表',
            component: './menu1/menu1sub1/menu1sub1list.tsx',
            hideInMenu: true,
          },
          {
            path: '/menu1/sub1/detail',
            name: '菜单分类一功能一详情',
            component: './menu1/menu1sub1/menu1sub1listdetail.tsx',
            hideInMenu: true,
          },
        ],
      },
      {
        path: '/menu1/sub2',
        name: '菜单分类一功能二列表',
        routes: [
          {
            path: '/menu1/sub2',
            name: '菜单分类一功能二列表',
            component: './menu1/menu1sub2/menu1sub2list.tsx',
            hideInMenu: true,
          },
          {
            path: '/menu1/sub2/detail',
            name: '菜单分类一功能二详情',
            component: './menu1/menu1sub2/menu1sub2listdetail.tsx',
            hideInMenu: true,
          },
        ],
      },
    ],
  }

但这个结果还是存在两个问题:
1、冗余配置太多
2、顶级路由重定向的菜单无法被点亮

复现过程:
使用这个仓库 https://github.com/rawbin-/antdpro-umi-router-debug
使用 yarn && yarn dev 可以复现,必要时回退代码到指定版本
image

预期:
有没有一个更简单合适的配置路由层级且能解决问题的方法?
另外,尝试过程中,发现 exact 配置也会导致路由不渲染,这个是不是也有类似的问题

@rawbin- rawbin- changed the title 多层级路由+重定向配置冗余,实现不了三级菜单需求 多层级路由+重定向配置冗余,实现三级菜单需求 Feb 10, 2022
@sorrycc sorrycc closed this as completed Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants