reactjs - React 路由将子路由移动到单独的模块?

标签 reactjs react-router react-router-redux

是否可以使用 React Route 执行以下操作?

主要

  <Routes handler={App}>
    <Profile path="profile" />
    <Explore path="explore" />
  </Routes>

简介

    <Route>
      <Route name="dashboard" handler={ProfileDashboard} />
      <Route name="repos" handler={ProfileRepos} />
    </Route>

探索

    <Route>
      <Route name="home" handler={ExploreHome} />
      <Route name="showcase" handler={ExploreShowcase} />
    </Route>

最佳答案

您可以使用 React Router 来实现这一点! :)

但我建议您检查一下“简单路由”方式来配置您的路由:

https://github.com/ReactTraining/react-router/blob/v2.8.1/docs/guides/RouteConfiguration.md#configuration-with-plain-routes

使用它,您将开始使用routes对象,并且您可以需要另一个路线并根据这些组合创建您的路线。类似这样的事情:

const routes = {
    path: '/',
    component: App,
    childRoutes: [
        require('./profile'),
        require('./explore')
    ]
}

然后在您的 profile.js (您可以对 explore.js 执行相同操作)文件中,您将看到类似的内容:

/* Import the ProfileDashboard and ProfileRepos here */

const profileRoutes = {
    path: 'profile',
    childRoutes: [{
        path: 'dashboard',
        component: ProfileDashboard
    }, {
        path: 'repos',
        component: ProfileRepos
    }]
};

这样你就可以实现你想要的。

如果你确实不能使用普通路由,你可以这样做:

<Route path="/" component={App}>
    { require('./profile') }
    { require('./explore') }
</Route>

还有你的profile.js,例如:

module.exports = (
    <Route path="profile">
        <Route path="dashboard" component={ProfileDashboard} />
        <Route path="dashboard" component={ProfileRepos} />
    </Route>
);

我不知道您使用的 React Router 版本是什么,但您可以在任何版本中实现这一目标,但是,作为建议,请尝试使用最新版本。因为它可以处理很多很酷的东西。

希望对你有帮助!

关于reactjs - React 路由将子路由移动到单独的模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38165107/

相关文章:

reactjs - react -路由器-dom v6 : What would be the equivalent to Route's render prop?

reactjs - React Router 的 useParams 导致 useEffect 重新运行

reactjs - redux-react-route v4/v5 push() 在 thunk Action 中不起作用

redux - 使用 ReactRouter v4 和 redux 将语言环境作为 URL 参数

javascript - 回调渲染丢失子 react 路由器和 redux 的路由器上下文

javascript - 了解 React Webpack bundle

reactjs - 为什么我的 JSX HTML 标签在编辑器中被分成多行?

javascript - 如何在路由更改时卸载组件

javascript - 多个下拉状态和处理 React 中的外部点击

reactjs - react : Is it okay if useCallback returns a value, 还是这是一个不好的模式?