javascript - 不要将 URL 添加到浏览器历史记录中

标签 javascript reactjs browser-history next.js

我阅读了有关浏览器历史记录的信息,并且了解我无权访问历史对象数组,更重要的是我无法删除它们。但是,我的任务是排除将参数化 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/

相关文章:

javascript - strip 化 IBAN 元素错误 'The type ` sepa_debit` 不是有效的源类型。

javascript - 如果从服务器接收到值,在哪里更新 Hook 的值更好?

reactjs - RTK查询错误: could not find react-redux context value; please ensure the component is wrapped in a <Provider>

javascript - 在 Material-ui 和 Radium 中使用多种样式的问题

javascript - 推送状态和弹出状态 : Safari 5 not returning window. 历史记录.状态

javascript - jQuery 阶梯滑出列表效果

javascript - 使用 joi + express 匹配目录路径的正则表达式

JavaScript 网页版本比较

javascript - 除了浏览器本身之外,如何使用 JavaScript 或 Java 清除浏览器(IE、Firefox、Opera、Chrome)历史记录?

javascript - jQuery 的加载功能,不允许后退按钮?