【发布时间】:2020-02-12 18:37:42
【问题描述】:
我想在单个路由文件中实现嵌套路由和侧边栏路由的某种组合。 我希望组件像这样呈现。
"/" --- Registration page
"/login" ---- Login Page
"/application" --- Application Page
"/dashboard" --- Dashboard page with Sidebar and Application components
"/dashboard/application" --- Dashboard page with Sidebar and Application components
"/dashboard/user" --- Dashboard page with Sidebar and User components
我的路线设置是这样
<Switch>
<Route path="/" component={withAuth(Registration)} />
<Route path="/login" component={withAuth(Login)} />
<Route path={'/dashboard'} component={withAuth(Dashboard)}/>
</Switch>
在仪表板页面中,我基本上实现了来自 react-router-dom 网站的sidebar example。
它工作正常,但是我想将所有路由放在一个文件中。
类似的东西
<Switch>
<Route path="/" component={withAuth(Registration)} />
<Route path="/login" component={withAuth(Login)} />
<Route path={'/dashboard'} component={withAuth(Dashboard)} children={[Sidebar, Application]}/>
<Route path="/dashboard/application" component={withAuth(Dashboard)} children={[Sidebar, Application]}/>
<Route path="/dashboard/user" component={withAuth(Dashboard)} children={[Sidebar, User]}/>
</Switch>
上面的代码是虚构的代码,但它是为了让我了解我想要实现的目标。我尝试了类似 question 的解决方案,但它对我不起作用。
如何将所有路由放在一个文件中,并在 react-router-dom v4/v5 中处理侧边栏路由和嵌套路由?
我创建了一个example code sandbox,请尝试this 示例。
【问题讨论】:
-
你到底想要什么?你能分享屏幕截图以便更好地澄清吗?
-
@VahidAkhtar 我已经包含了示例代码。在代码中读取 cmets。 codesandbox.io/s/react-router-nesting-n7cdc
-
我在一个闭源产品中做过类似的事情。但是,我认为答案是您不能将所有路由放入 1 个文件中。我的路由分布在多个文件中,类似于您上面的文件,每个文件中都有一个开关。我构造它的方式是,每个路由的父组件都包含开关,并作为 Web 应用程序该部分的子菜单的路由器运行。
标签: reactjs routing react-router react-router-v4 react-router-dom