如何在React JS功能组件中实现儿童/嵌套路由
我有以下路由配置
<Routes>
{
navbarRoutes.map((route) => (
<Route key={route.path} path={route.path} element={route.main}>
))
}
</Routes>
和路由常数文件的app.js如下:
const navbarRoutes = [
{
path: '/',
text: ''user,
exact: true,
main: <User>
},
{
path: '/useraddress',
text: ''useraddress,
exact: true,
main: <useraddress>
}
]
如何在“/userAddress”内部以及app.js内部实现嵌套路由或子路由?
I have App.js with following route configuration
<Routes>
{
navbarRoutes.map((route) => (
<Route key={route.path} path={route.path} element={route.main}>
))
}
</Routes>
and route constant file is as follows
const navbarRoutes = [
{
path: '/',
text: ''user,
exact: true,
main: <User>
},
{
path: '/useraddress',
text: ''useraddress,
exact: true,
main: <useraddress>
}
]
How can I implement nested route or child route inside "/useraddress" and inside App.js as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,这是您的解决方案:
我认为您的问题是不要称呼这样的组件。
基本上,我们有这样的纳维尔路径:
路径和一个元素(顺便说一下,我认为我们不再需要确切的属性)。然后,我们将其映射到放置browserrouter的位置,
您可以在此 codesandbox ,索引有一个路由 /周围,另一个路由:)
So here is your solution :
I think that your problem was to not call the Component like this <route.main />
Basically we have navbarRoutes like this :
A path and an element (by the way I think that we don't need the exact property no more). Then we map it where we have put our BrowserRouter
You can try it out on this codeSandbox, there is a route /about and another one at the index :)
使用此模式,也可以手动进行。不要使用循环
Use this pattern and also do it manually. Don't use loop