简体   繁体   English

在 Angular 12 中禁用浏览器后退按钮

[英]Disable Browser Back Button in Angular 12

I am developing a SPA using Angular 12.我正在使用 Angular 12 开发 SPA。

Users do not directly load this web application, they open another web application first and then redirect to the new SPA.用户不直接加载这个 web 应用程序,他们先打开另一个 web 应用程序,然后重定向到新的 SPA。

In the new SPA, I want to disable the browser back button to stop navigating away.在新的 SPA 中,我想禁用浏览器后退按钮以停止导航。 New SPA has all the control if the user really wants to go back.如果用户真的想要 go 回来,新的 SPA 拥有所有控制权。

I have used the below code to disable the back button and but it doesn't work on the initial page that the user gets after the first redirection.我已使用以下代码禁用后退按钮,但它在用户第一次重定向后获得的初始页面上不起作用。 From there onwards, the back button is disabled.从那里开始,后退按钮被禁用。

app.component.ts app.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
  ngOnInit(): void {
    history.pushState(null, '');
  }
}

Because of the redirection browser knows the previous place and allows to go back.因为重定向浏览器知道以前的地方并允许 go 回来。

Do you know how can I fix this issue?你知道我该如何解决这个问题吗?

Thanks and regards,谢谢并恭祝安康,

Ash

This is a chrome behavior that we cannot disable the back button if the user has not done any interaction with the page.这是一种 chrome 行为,如果用户没有与页面进行任何交互,我们无法禁用后退按钮。

Getting this working from chrome让这个工作从 chrome

[Intervention] Blocked attempt to show a 'beforeunload' confirmation panel for a frame that never had a user gesture since its load. [干预] 阻止尝试为自加载以来从未有过用户手势的框架显示“卸载前”确认面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM