如何限制用户在浏览器中按下后退按钮|角6

问题描述 投票:1回答:1

我正在为我的项目使用Angular 6。我需要限制用户点击浏览器中的后退按钮并显示警告消息。

angular browser
1个回答
1
投票

您需要编写一个类来实现“CanDeactivate”界面,您可以在其中处理事件。尝试使用后退按钮事件中的location.go()重定向到活动URL本身。

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;
        }
    }
}

我没有运行上面的代码。它只是一个想法,所以你可以开始。让我稍后检查和更新。

© www.soinside.com 2019 - 2024. All rights reserved.