Vidya
Vidya

Reputation: 126

How to detect browser back click in angular 4

Anyone please resolve my problem. I need to write If condition for browser back button. I have tried so many ways but it not giving the exact result.

if (window.performance && window.performance.navigation.type == window.performance.navigation.TYPE_BACK_FORWARD) {
}

In the above code window.performance.navigation.type is always returning the value 1.

And also tried ngOnDestroy().

Upvotes: 2

Views: 1548

Answers (1)

Krishna Rathore
Krishna Rathore

Reputation: 9687

you can try with HostListener

@HostListener('window:popstate', ['$event'])
  onPopState(event) {
    console.log('Back button clicked');
}

Upvotes: 2

Related Questions