typescript - 从嵌套路由路由时,nextjs 中的不可预测路由
问题描述
我创建了一个名为dashboard
pages 文件夹的文件夹,其中包含以下文件:
- 索引.tsx
- 客户.tsx
- 发票.tsx
- 物品.tsx
当用户路由到http://localhost:3000/dashboard
页面index.tsx
被渲染时。但是,当我单击一个按钮并路由到发票或客户404 page
时,当我检查它路由到的 url 时,我得到一个http://localhost:3000/customers
,而http://localhost:3000/invoices
不是http://localhost:3000/dashboard/customers
andhttp://localhost:3000/dashboard/invoices
但是,当我尝试在 href 中为客户编写完整路线时:
//It will work after refreshing the page
<Link href='dashboard/customers'>
<a>Customers</a>
</Link>
当我尝试将任何路由从客户路由到任何嵌套页面时,例如:
<Link href='dashboard/invoices'>
<a>Customers</a>
</Link>
// I tend to get a 404 page
当我检查网址时,它执行了以下操作:http://localhost:3000/dashboard/dashboard/invoices
而不是http://localhost:3000/dashboard/invoices
下面的按钮代码:
<Link href='/customers'>
<a>Customers</a>
</Link>
<Link href='/invoices'>
<a>Invoices</a>
</Link>
真的不知道我做错了什么
解决方案
<Link href='dashboard/invoices' />
是一条相对路线——href='dashboard/invoices'
去往的意思/yourCurrentLocation/dashboard/invoices
。
对于绝对路由,您必须在前面加上/
. 这意味着它将始终相对于根目录进行路由。因此,您的代码应该是<Link href='/dashboard/invoices' />
.
推荐阅读
- c# - No parameterless constructor defined for this object error after adding code-base EF configuration
- r - fill NA in timeseries from same date and time, different years r
- c++ - 为什么 Visual Studio 会尝试注册我的 DLL
- android - how to start a background service in oreo?
- ios - NotificationCenter Selector 方法未在 Swift 中调用
- linux - Is there a way to "carry around" my own sh functions inside linux terminals, without having to install a file everywhere I connect?
- .net - XElement.WriteTo(XmlWriter) 和 XElement.Save(XmlWriter) 有什么区别?
- sql - 使用 CASE 语句将行值转换为结果中的列
- java - XStream Converter Context convertAnother loses attributes
- c# - MYSQL 巨大的记录并找到每个最近的点