0

I am trying to redirect the user to a particular url on the click of back button in browser, and here is the code

constructor(private router: Router,private location: PlatformLocation) {        
        let eventUrl = window.sessionStorage.getItem('Event');
        this.location.onPopState(() => {
            alert('click');
            this.router.navigate(['/', this.selectedLanguage, 'event', eventUrl]);
        });
    }

but it does not seem to fire the event when user click on back button. The code is in the current component. what is the issue?

EDIT

I tried with the below answer suggested, but that too does not seems to work, the function getting called , but it still redirects to the previous page rather than the mentioned url below

unload(event: any) {
        let eventUrl = window.sessionStorage.getItem('buyerEvent');
        this.router.navigateByUrl('/' + this.selectedLanguage + '/event/' + eventUrl);
    }
4

2 回答 2

2

您可以在将应用程序留给window.unload事件时绑定您想做的事情。这里我使用了HostListener.

@HostListener('window: unload', ['$event'])
unload(event) {
  window.open('https://localhost:4200', '_blank');
  window.close();
}

提到 Chrome 会window.open默认阻止,您必须更改阻止策略。

于 2017-08-15T07:09:56.223 回答
-1

我正在使用位置

Import { Location } from '@angular/common';

在构造函数上声明

constructor(private _location: Location){}

并做一个功能

backClicked() {
    this._location.back();
}

然后将其设置为按钮

<button (click)="backClicked()"> TEAM FALL BACK</button>

然后看魔术发生...

于 2017-08-15T06:05:16.123 回答