我正在通过导入使用下一个路由器useRouter
from next/router
.
我正在尝试找到一种解决方案,当我更改 URL 的查询时,该解决方案不会滚动到页面顶部。有什么解决办法吗?我知道 Next 的 Link 组件有这个选项,但我需要使用 Router 组件。我的下一个版本是 10.0.5。
const router = useRouter();
const changeCurrency = (newCurrency) => {
//Do some stuff here
Router.push({
pathname: router.pathname,
query: { ...router.query, currency: newCurrency.value },
});
};
router.push
has a scroll
选项,它是true
默认情况下。您可以像这样关闭它:
const router = useRouter();
async function navigate(newCurrency) {
router.push({
pathname: router.pathname,
query: { ...router.query, currency: newCurrency.value },
}, undefined, { scroll: false });
}
router.push
接受大部分(如果不是全部)next/link
选项对象中的 props。您可以在这里检查它们:https://nextjs.org/docs/api-reference/next/link https://nextjs.org/docs/api-reference/next/link
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)