我正在使用angular6。我有多个页面,并且只想在一页上禁用浏览器。我为此使用了window.onpopstate = function (e) { window.history.forward(); };
,它使浏览器对所有页面都禁用,因为当我在其他页面上单击并单击浏览器后也使浏览器返回禁用,但是我想使仅在一页上禁用。如何在Angular中仅对一页禁用浏览器返回,而对其余页面禁用浏览器?
notification-approved-by-id.component.ts
import { Component, OnInit } from '@angular/core';
import { StatusService } from '../service/status.service';
import { Notification } from '../classes/notification';
import { Router } from '@angular/router';
@Component({
selector: 'app-notific-approved-by-id',
templateUrl: './notific-approved-by-id.component.html',
styleUrls: ['./notific-approved-by-id.component.css']
})
export class NotificApprovedByIdComponent implements OnInit {
notificationObj : Notification = new Notification();
ngOnInit() {
var em;
this.statusService.getNotificationApprovedById(em).subscribe(
(data) => {
if(data != undefined && data.payload != undefined && data.status == 1){
this.notificationObj = data.payload[0];
window.onpopstate = function (e) { window.history.forward(); };
}
}
);
}
}
答案 0 :(得分:1)
您必须在破坏页面的同时删除侦听器
export class NotificApprovedByIdComponent implements OnInit {
constructor(private statusService : StatusService,private router : Router) { }
const orig_onpopstate: any = window.onpopstate;
ngOnInit() {
var em;
this.statusService.getNotificationApprovedById(em).subscribe(
(data) => {
if(data != undefined && data.payload != undefined && data.status == 1){
window.onpopstate = function (e) { window.history.forward(); };
}
});
}
ngOnDestroy(): void {
window.onpopstate = orig_onpopstate;
}