NextJS路线在URL中显示[slug]

发布于 2025-02-08 13:08:23 字数 526 浏览 2 评论 0原文

我有一个带有文件夹结构的NextJS应用程序:

- pages
  - docs
   - [slug]
     - index
     - [id]
       - index

这些页面应该基于UI中出现的数据是动态的。这意味着用户可以转到/文档/付款/简介(其中付款简介也是动态页面)。

但是,当用户在嵌套深处(文档/付款)中的一个级别时,一切正常,但是当用户达到2级深(例如DOC/付款/简介)时,所有链接都开始在其URL中显示[SLUG]。假设侧边栏中有一个链接,应该转到/doc/payments/payments/faq它开始在URL中显示/docs/[slug]/faq这是一个示例URL

我不知道它的发生率。

I have a NextJS app with the folder structure:

- pages
  - docs
   - [slug]
     - index
     - [id]
       - index

These pages are supposed to be dynamic based on the data that appear in the UI. Meaning a user can go to /docs/payments/introduction (where payments and introduction are dynamic pages too).

However everything works fine when the user is one level deep in the nesting eg (docs/payments), but when the user goes 2 level deep (eg docs/payments/introduction), all the links begin showing [slug] in their url. Let's say there's a link in the sidebar that's supposed to go to /docs/payments/faq it then starts showing /docs/[slug]/faq in the url.
Here's a screenshot of an example url

I have no idea how its happening.

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

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

发布评论

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

评论(2

时光病人 2025-02-15 13:08:23

您需要使用[... slug] .tsx才能使用多个sl。

- pages
  - docs
   - [...slug].tsx

如果需要更多信息,请单击在这里

You need to use [...slug].tsx to be able to use multiple slugs.

- pages
  - docs
   - [...slug].tsx

If you want more info, click here.

夏至、离别 2025-02-15 13:08:23

您确定正确使用链接 s和router.push es e e es吗?使用动态sl时,应将用作 prop。这样:

<Link as="/page/docs/getting-started/creating-an-account" to="/page/docs/[slug]/[id]">
  <a>Link</a>
</Link>

或者,如果您使用路由器,则是这样:

const router = useRouter();

router.push('/page/docs/[slug]/[id]', '/page/docs/getting-started/creating-an-account');

Are you sure you are using Links and router.pushes correctly? When using dynamic slugs, you should use as prop. Like this:

<Link as="/page/docs/getting-started/creating-an-account" to="/page/docs/[slug]/[id]">
  <a>Link</a>
</Link>

or if you are using router, like this:

const router = useRouter();

router.push('/page/docs/[slug]/[id]', '/page/docs/getting-started/creating-an-account');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文