我的目标是禁用浏览器窗口的后退按钮。因此,在我的组件中,我正在尝试侦听路径,因为我已经从angular / common导入了位置并尝试订阅它,但它似乎并不适合我。这是我想要禁用后退按钮的那个特定组件的代码片段。
import { DataService } from './../../../shared/services/data.service';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Location } from "@angular/common";
@Component({
selector: 'app-confirmation',
templateUrl: './confirmation.component.html',
styleUrls: ['./confirmation.component.scss']
})
export class ConfirmationComponent implements OnInit {
userInput;
customWindow;
time;
message: string;
constructor(private location: Location, private dataService: DataService, private router: Router) {
this.dataService.currentMessage
.subscribe(message => (this.message = message));
console.log(this.message);
this.dataService.timeSource.subscribe(time => (this.time = time));
console.log(this.time);
}
ngOnInit(): void {
this.customWindow = window.open("", "_blank", "");
console.log('here');
console.log(this.location);
this.location.subscribe(x => console.log(x));
}
close () {
console.log(this.customWindow);
// this.customWindow.close();
console.log('666');
// this.router.navigate(['login']);
}
}
答案 0 :(得分:0)
有一个纯Javascript的解决方案,不需要Angular方法。
如下所示:Disable browser back button for one page application
您的解决方案如下所示:
window.onpopstate = function (e) { window.history.forward(1); }
但是在Angular Universal中执行此代码时要小心。