美文网首页
路由传递params参数(useParams和useRouteM

路由传递params参数(useParams和useRouteM

作者: LXEP | 来源:发表于2022-12-13 14:51 被阅读0次

    umi3采用的是都是React Router5,hooks传递参数使用useParams和useRouteMatch等方式。

    但是父级路由是无法访问子路由的参数的,比如

    {
        path: '/parent',
        component: '@/components/ParentLayout',
        routes: [
          {
            path: '/parent/info/:id',
            component: './parent/Info',
          },
          {
            path: '/parent/optInfo/:id',
            component: './parent/OptInfo',
          },
        ],
      },
    

    应改为:

    {
        path: '/parent/:type/:id',
        component: '@/components/ParentLayout',
        routes: [
          {
            path: '/parent/info/:id',
            component: './parent/Info',
          },
          {
            path: '/parent/optInfo/:id',
            component: './parent/OptInfo',
          },
        ],
      },
    

    通过useParams获取参数

    import { useParams, useRouteMatch } from 'umi'
    console.log(useParams())
    

    相关文章

      网友评论

          本文标题:路由传递params参数(useParams和useRouteM

          本文链接:https://www.haomeiwen.com/subject/yzphqdtx.html