禁用后退按钮,刷新 Angular 11 中的 webApp。这是保存当前页面所需的 MCQ 类型问题,并且仅限于 moove prev

Posted

技术标签:

【中文标题】禁用后退按钮,刷新 Angular 11 中的 webApp。这是保存当前页面所需的 MCQ 类型问题,并且仅限于 moove prev【英文标题】:Disable the back button, refresh for a webApp in Angular 11.This is MCQ type question required to save the current page and restricted to moove prev 【发布时间】:2022-01-15 01:55:02 【问题描述】:

如何使用 Angular 11 阻止用户返回、刷新和不保存当前页面。非常感谢任何建议。

我尝试了以下脚本,但它并没有禁用后退按钮、刷新和提交。

感谢和问候。

history.push State(null, null, window.location.href);

this.Location.onPopState(() =>

history.pushState(null, null, window.location.href);

this.stepper.previous();

【问题讨论】:

这能回答你的问题吗? How can I stop the browser back button using javascript? 和/或 How to disable the back button in the browser using JavaScript 另请注意How do I format my posts using Markdown or html?。与 字符数相比,您的代码中的 字符数不相等。 (并且格式不正确......) 【参考方案1】:

您无法真正禁用浏览器的后退按钮,就像您无法阻止用户关闭窗口/选项卡并重新打开前一页一样。但是,您可以在离开页面之前使用 onbeforeunload-事件:

window.addEventListener('beforeunload', (event) => 
  if (thereAreChanges) 
    event.returnValue = 'Please save your changes before leaving this page';
  
);

【讨论】:

以上是关于禁用后退按钮,刷新 Angular 11 中的 webApp。这是保存当前页面所需的 MCQ 类型问题,并且仅限于 moove prev的主要内容,如果未能解决你的问题,请参考以下文章