我阅读了有关浏览器历史记录的信息,并且了解我无权访问历史对象数组,更重要的是我无法删除它们。但是,我的任务是排除将参数化 URL 添加到历史记录中。我的项目中有选项卡,它们的 URL:
/profile,
/profile?menu=1
/profile?menu=2
我在我的项目中使用nextjs框架。他的导航代码:
public handleOnClickItem(key: string) {
const id = '1';
const href = `/profile?id=${id}&menu=${key}`;
Router.push(href, href, { shallow: true });
// tabs are navigated
Router.onBeforeHistoryChange = (url) => {
console.log('App is changing to before history change: ', url);
};
}
我可以不将此网址添加到历史记录中吗?我在 console.log 中得到它。正如我从该方法的描述中了解到的,这个 URL 不应该被载入史册。 有任何想法吗?谢谢!
最佳答案
这应该可以解决它:
https://nextjs.org/docs/api-reference/next/router#routerreplace
Similar to the replace prop in next/link, router.replace will prevent adding a new URL entry into the history stack.
同时请记住,浅层路由选项用于在执行路由时触发/不触发 next.js 生命周期方法(例如 getInitialProps)。
关于javascript - 不要将 URL 添加到浏览器历史记录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56595680/