如何在不重新加载 NextJs 中的整个页面的情况下导航到另一个页面?

How to navigate to another page without reloading the whole page in NextJs?

我注意到在 NextJs 中,每当我单击 <Link> 转到另一个页面时,它都会调用 getInitialProps,即使那个 "another" 页面是同一页面。

例如,在 /profile 页面上,有 2 个组件,一个用于 "user information",另一个组件用于 "list of orders",已分页。 "user information" 和 "list of orders" 组件中使用的数据都是从 /profile 页面的 getInitialProps 中的 API 中获取的。

当我在 /profile 页面上单击转到 "list of orders" 组件的下一页时,它会转到 /profile/p/2。数据加载并且一切正常,除了 /profile/p/2 实际上重新加载整个个人资料页面并重新获取本应保持不变的 "user information" 数据(并且不需要重新获取) 只要页面是 /profile 页面的一部分。

有没有办法从 /profile 导航到 /profile/p/2 而无需重新加载整个页面,而只加载显示订单列表的组件?

您可以像这样将 shallow 传递给 Link

<Link href="...." as="...." shallow=true />