NextJS:与嵌套动态路由相关的问题(/category/[label]/subcategory/[id])

发布于 2025-02-06 09:23:04 字数 1180 浏览 1 评论 0 原文

类别路径

http://localhost:3000/category/Municipality

选择类别[label]时的

http://localhost:3000/category/subcategory/44

,然后选择子类别[id],我在路径中失去了值[label],它只会向我显示以下路径: NextJS允许我在没有问题的情况下显示子类别信息,但是它所采用的路径不正确。

我想要这样的路:

http://localhost:3000/category/Municipality/subcategory/44

代码:示例


  const path = `/category/subcategory/${directory?.id}`; // ??

  return (
    <List.Item key={directory.id}>
      <Skeleton avatar title={false} loading={isLoading} active>
        <Card cover={<Image src={`${config.SERVER_URL}${thumbnail?.attributes?.formats?.large?.url}`} />} style={{ width: 300 }} loading={isLoading}>
          <Meta
            title={<Link href={path}>{title}</Link>} // path to view subcategory information
            description={description}
          />
        </Card>

      </Skeleton>
    </List.Item>
  );
}

enter image description here

Category Path

http://localhost:3000/category/Municipality

When selecting a category [label] and then selecting the subcategory [id], I am losing the value [label] in the path, and it only shows me the following path:

http://localhost:3000/category/subcategory/44

Nextjs allows me to display the subcategory information without problems, but the path it is taking is not correct.

I want the path to be like this:

http://localhost:3000/category/Municipality/subcategory/44

Code: Example


  const path = `/category/subcategory/${directory?.id}`; // ??

  return (
    <List.Item key={directory.id}>
      <Skeleton avatar title={false} loading={isLoading} active>
        <Card cover={<Image src={`${config.SERVER_URL}${thumbnail?.attributes?.formats?.large?.url}`} />} style={{ width: 300 }} loading={isLoading}>
          <Meta
            title={<Link href={path}>{title}</Link>} // path to view subcategory information
            description={description}
          />
        </Card>

      </Skeleton>
    </List.Item>
  );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

魄砕の薆 2025-02-13 09:23:04

您需要稍微更改文件夹结构,然后将其如下:

  • 页面:页面
    • 类别
      • [label] &lt; - 这是一个文件夹
        • 子类别
          • [id] .tsx
        • index.tsx

在这里也是指向工作示例的链接

,也是您的代码示例中的 path 变量看起来不正确。我相信那应该是那样的

const path = `/category/${category.label}/subcategory/${directory?.id}`;

You need to change the folder structure a little bit and make it like the following:

  • pages
    • category
      • [label] <-- this is a folder
        • subcategory
          • [id].tsx
        • index.tsx

Here is the link to the working example

Also in your code example, the path variable looks incorrect. I believe it should be something like that

const path = `/category/${category.label}/subcategory/${directory?.id}`;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文