我正在尝试在React Redux项目中仅针对特定页面阻止浏览器后退按钮。 假设我有四个页面,用户可以在其中所有页面间来回切换,除了“page4”。一旦用户访问了“page3”并点击了下一步按钮,他就无法返回。我尝试了多种选项,但没有一个如预期的工作。以下代码符合我的预期,但会阻止所有页面的浏览器后退。
componentDidMount() {
window.history.pushState(null, document.title, window.location.href);
window.addEventListener('popstate', function (event){
window.history.pushState(null, document.title, window.location.href);
});
}