如何根据嵌套路线创建正确的URL?

发布于 2025-01-24 01:02:57 字数 3367 浏览 1 评论 0原文

我正在尝试使用用户量钩子在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深巷少女 2025-01-31 01:02:57

我找到了解决问题的解决方案:

我将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 HeaderLayout from "./Components/HeaderLayout";

const routes = [
  {
    path: "/",
    element: <HeaderLayout />,
    children: [
      {
        path: "/titles",
        element: <Outlet />,
        children: [
          {
            index: true,
            element: <Titles />
          },
          { path: ":id", element: <TitleDetails /> },
          { path: ":id/materials", element: <Materials /> },
          { path: ":id/materials/:idMaterial", element: <MaterialDetails /> }
        ]
      }
    ]
  }
];
export default routes;

然后在titledetails.js中,我删除了//code>/材料

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>
    </>
  );
}

I found the solution to my problem:

I changed routes.js to:

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 HeaderLayout from "./Components/HeaderLayout";

const routes = [
  {
    path: "/",
    element: <HeaderLayout />,
    children: [
      {
        path: "/titles",
        element: <Outlet />,
        children: [
          {
            index: true,
            element: <Titles />
          },
          { path: ":id", element: <TitleDetails /> },
          { path: ":id/materials", element: <Materials /> },
          { path: ":id/materials/:idMaterial", element: <MaterialDetails /> }
        ]
      }
    ]
  }
];
export default routes;

and then in TitleDetails.js I removed the / 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>
    </>
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文