首页 > 解决方案 > 如何正确合并 react-router-config 数组?

问题描述

我正在查看https://www.npmjs.com/package/react-router-config

我想用它来构建我的路由配置,但是我的路由配置将来自其中的多个包,node_modules并且需要在使用前正确合并。

路由配置形状

路由是与 a 具有相同属性的对象,但<Route>有几个不同之处:

const routes = [
  { component: Root,
    routes: [
      { path: '/',
        exact: true,
        component: Home
      },
      { path: '/child/:id',
        component: Child,
        routes: [
          { path: '/child/:id/grand-child',
            component: GrandChild
          }
        ]
      }
    ]
  }
]

因此,如果我要合并第二条路线配置:

const routes = [
  { component: Root,
    name: 'root',
    routes: [
      { path: '/',
        exact: true,
        component: Home
      },
      { path: '/child/:id',
        name: 'childView',
        routes: [
          { path: '/child/:id/related-child',
            component: RelatedChild
          }
        ]
      }
    ]
  }
]

我想与每个嵌套路由的所有键都有一个对应关系,即合并。

我怎样才能做到这一点

我怎样才能做到这一点?

标签: javascriptarraysreactjsreact-routerreact-router-dom

解决方案


推荐阅读