如何限制用户在浏览器中按返回按钮?角度6 [英] How to restrict users from pressing back button in browser | angular 6
本文介绍了如何限制用户在浏览器中按返回按钮?角度6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为项目使用Angular 6。我需要限制用户单击浏览器中的后退按钮,而改为显示警报消息。
I am using Angular 6 for my project. I need to restrict users from clicking on the back button in browser and show alert message instead.
推荐答案
您需要编写一个类实现 CanDeactivate 界面,您可以在其中处理事件。尝试在后退按钮事件中使用 location.go()重定向到活动URL本身。
You need to write a class to implement "CanDeactivate" interface where you can handle the events. Try redirecting to the active URL itself with location.go() like this on the back button event.
location .go('getTheCurrentPathToRedirect');
类似于以下内容:
export class CanDeactivateBack implements CanDeactivate<any> {
constructor(public location: Location, public router: Router) {}
canDeactivate(component: any, currentRoute: ActivatedRouteSnapshot): boolean {
//YourConditionIfBackButttonPress - check for the history back click
if (YourConditionIfBackButttonPress) {
let urlPath = this.router.createUrlTree([], currentRoute);
let curUrlPath = urlPath.toString();
this.location.go(curUrlPath);
return false;
} else {
return true;
}
}
}
我还没有运行以上代码。它只是一个想法,因此您可以从开始。让我检查并稍后更新。
I haven't run the above code. Its just an Idea so you can start with. Let me check and update later.
这篇关于如何限制用户在浏览器中按返回按钮?角度6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文