Search code examples
angularangular2-routing

chrome exits the angular app on clicking hardware backbutton


i have setup my route links like this

  1. matches screen

http://localhost:4200/#/nav/matches

  1. my profile

http://localhost:4200/#/nav/my-profile

  1. edit profile

http://localhost:4200/#/nav/edit-profile

  1. my photos

http://localhost:4200/#/nav/my-photos

  1. settings

http://localhost:4200/#/nav/settings

  1. my orders

http://localhost:4200/#/nav/my-orders

the problem iam facing is whenever i clicks the hardware back button after navigation through all these pages

app exits to goole default screen or some other url i searched before (like https://stackoverflow.com/) some times it comes to matches screen which is my default screen and exits ( navigating away from my site )

i want to go back to previous page of my application instead of exiting directly like

5 ==> 4==> 3 ==> 2 ==> 1 ==> exit


Solution

  • The unexpected behaviour is because in my project to navigate to another routes we were using

        this.router.navigateByUrl(link, {replaceUrl: true});
    

    this replaces history thanks to Angular 2 - replace history instead of pushing

    then i changed that to

        this.router.navigateByUrl(link);
    

    and the issue fixed