Angular 6 - Back button press trigger more than once

11 views
Skip to first unread message

shin

unread,
Apr 8, 2019, 9:09:13 AM4/8/19
to Angular and AngularJS discussion
I have the following code to detect the back button press using angular 6.

import { Location } from '@angular/common';
export class ProductsComponent implements OnInit {

constructor( private location: Location){
  this.handleBackButtonPress();
}
  handleBackButtonPress() {
    this.subscribed = true;
    this.location.subscribe(redirect => {
     if (redirect.pop === true) {
      alert('this is a backbutton click');
     }
    });
  }
}

This is working and we got alert on back button press. The problem is If we visit the same page more than once it will trigger the alert with the number of time we visited the route with the same component.
Reply all
Reply to author
Forward
0 new messages