如何根据嵌套路线创建正确的URL?
我正在尝试使用用户量
钩子在React项目中创建一些嵌套路由。但是我正在努力使url
按照我的意愿显示。
示例螺纹项目是一个:
- public
- src
- Components
- Header.js
- MaterialsDetails.js
- Materials.js
- TitleDetails.js
- Titles.js
- App.js
- index.js
- routes.js
我在routes.js
中创建了路由:
import { Outlet } from "react-router-dom";
import Titles from "./Components/Titles";
import TitleDetails from "./Components/TitleDetails";
import Materials from "./Components/Materials";
import MaterialDetails from "./Components/MaterialDetails";
import Header from "./Components/Header";
const routes = [
{
path: "/",
element: <Header />,
children: [
{
path: "/titles",
element: <Outlet />,
children: [
{
index: true,
element: <Titles />
},
{ path: ":id", element: <TitleDetails /> }
]
},
{
path: "/materials",
element: <Outlet />,
children: [
{
index: true,
element: <Materials />
},
{ path: ":id", element: <MaterialDetails /> }
]
}
]
}
];
export default routes;
headerlayout.js 将在每个页面中显示:
import React from "react";
import { Outlet } from "react-router-dom";
export default function HeaderLayout() {
return (
<>
<h1>Header Layout</h1>
<Outlet />
</>
);
}
然后,我有titles.js
我有一个标题列表,每个标题在单击它时都有其详细信息:
import React from "react";
import { Link } from "react-router-dom";
export default function Titles() {
return (
<>
<h3>Titles</h3>
<p>
<Link to="1">Title example 1</Link>
</p>
<p>
<Link to="2">Title example 2</Link>
</p>
<p>
<Link to="3">Title example 3</Link>
</p>
</>
);
}
然后在titledletails.jss.js
中具有标题详细信息,我有一个链接转到相关材料。因此,当我单击时,我将转到材料列表:
import React from "react";
import { useParams } from "react-router-dom";
import { Link } from "react-router-dom";
export default function TitleDetails() {
const { id } = useParams();
return (
<>
<h1>Details of title {id}</h1>
Related materials: <Link to="/materials">Materials</Link>
</>
);
}
最后,在材料中。当我单击一份材料列表时,我将转到
materialldetails.js
import React from "react";
import { Link } from "react-router-dom";
export default function Materials() {
return (
<>
<h3>Materials</h3>
<p>
<Link to="1">Material example 1</Link>
</p>
<p>
<Link to="2">Material example 2</Link>
</p>
<p>
<Link to="3">Material example 3</Link>
</p>
</>
);
}
问题是我如何修改doutes.js
以获取,例如,该URL: http:// localhost:3000/titles/1/材料/3
,而不是http:// localhost:3000/材料
当我在http中进行http时:// localhost:3000/titles/1
?
谢谢你!
I am trying to create some nested routes in a react project using the useRoutes
hook. But I am struggling to make the URL
show as I want.
The example struture project is this one:
- public
- src
- Components
- Header.js
- MaterialsDetails.js
- Materials.js
- TitleDetails.js
- Titles.js
- App.js
- index.js
- routes.js
I have create the routes in routes.js
like this:
import { Outlet } from "react-router-dom";
import Titles from "./Components/Titles";
import TitleDetails from "./Components/TitleDetails";
import Materials from "./Components/Materials";
import MaterialDetails from "./Components/MaterialDetails";
import Header from "./Components/Header";
const routes = [
{
path: "/",
element: <Header />,
children: [
{
path: "/titles",
element: <Outlet />,
children: [
{
index: true,
element: <Titles />
},
{ path: ":id", element: <TitleDetails /> }
]
},
{
path: "/materials",
element: <Outlet />,
children: [
{
index: true,
element: <Materials />
},
{ path: ":id", element: <MaterialDetails /> }
]
}
]
}
];
export default routes;
Then I have the HeaderLayout.js
that will be shown in every page:
import React from "react";
import { Outlet } from "react-router-dom";
export default function HeaderLayout() {
return (
<>
<h1>Header Layout</h1>
<Outlet />
</>
);
}
In titles.js
I have a list of titles where each title has its details when clicking on it:
import React from "react";
import { Link } from "react-router-dom";
export default function Titles() {
return (
<>
<h3>Titles</h3>
<p>
<Link to="1">Title example 1</Link>
</p>
<p>
<Link to="2">Title example 2</Link>
</p>
<p>
<Link to="3">Title example 3</Link>
</p>
</>
);
}
Then in TitleDetails.js
apart of having the title detail I have a link to go to the related materials. So when I click I will go to the list of materials:
import React from "react";
import { useParams } from "react-router-dom";
import { Link } from "react-router-dom";
export default function TitleDetails() {
const { id } = useParams();
return (
<>
<h1>Details of title {id}</h1>
Related materials: <Link to="/materials">Materials</Link>
</>
);
}
And finally in Materials.js
I have the same concept as Titles.js
. A list of materials that when I click on one I will go to MaterialDetails.js
import React from "react";
import { Link } from "react-router-dom";
export default function Materials() {
return (
<>
<h3>Materials</h3>
<p>
<Link to="1">Material example 1</Link>
</p>
<p>
<Link to="2">Material example 2</Link>
</p>
<p>
<Link to="3">Material example 3</Link>
</p>
</>
);
}
The question is how can I modify routes.js
to obtain, for example, that URL: http://localhost:3000/titles/1/materials/3
instead of going to http://localhost:3000/materials
when I amb in http://localhost:3000/titles/1
?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决问题的解决方案:
我将
routes.js
更改为:然后在titledetails.js中,我删除了
/
的/code>/材料
:I found the solution to my problem:
I changed
routes.js
to:and then in TitleDetails.js I removed the
/
of/materials
: