如何用react-router-dom实现二级路由?
容器组件
<Layout>
<Sider>
<Menu
onClick={this.handleMenuClick}
defaultSelectedKeys={['1']}
>
<Menu.Item key="1">
<Link to={{
pathname: "/machine/general",
}}>概况</Link>
</Menu.Item>
<Menu.Item key="2">
<Link>流量</Link>
</Menu.Item>
<Menu.Item key="3">
<Link>库存</Link>
</Menu.Item>
<Menu.Item key="4">
<Link>维护</Link>
</Menu.Item>
</Menu>
</Sider>
<Content>
<Switch>
{
machineRoute.map((item, index) => {
return (
<AuthRoute {...item} key={index} />
)
})
}
</Switch>
</Content>
</Layout>
AuthRoute
是一个控制权限的路由 其实就是返回Route
组件
具体如下:
render() {
if (this.passOrNot()) {
return (
<Route component={this.props.component} path={this.props.path} {...this.props}></Route>
)
} else {
return (
<Switch>
<Route component={rootRoute[0].component}></Route>
</Switch>
)
}
}
machineRoute
是配置路由信息的数组:
const route = [
{
path: '/machine/genaral',
component: general,
exact: true,
strict: true,
sensitive: true,
role: 10,
},
{
path: '/machine/maintain',
component: maintain,
exact: true,
strict: true,
sensitive: true,
role: 10,
},
{
path: '/machine/flow',
component: flow,
exact: true,
strict: true,
sensitive: true,
role: 10,
},
{
path: '/machine/stock',
component: stock,
exact: true,
strict: true,
sensitive: true,
role: 10,
}
]
export default route;
页面左边是菜单,右边是内容
在这个页面的外面还有一层BrowserRouter
包着
在点击菜单以后,现在会重新打开一个新页面
而不是在Content
下的一个子页面
如何解决?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)