我试图仅在 react redux 项目中阻止特定页面的浏览器后退按钮。假设我有四页。
http://localhost:3000/abc/#/page1,
http://localhost:3000/abc/#/page2,
http://localhost:3000/abc/#/page3
和 http://localhost:3000/abc/#/page4
等等
对于所有页面,除了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);
});
}
Run Code Online (Sandbox Code Playgroud)