Search code examples
angularangular6angular8angular-routingangular-router

Cant leave page on backbutton after entering with queryParams Angular


im having trouble trying to leave the page whit pagination on a list.

original route => path: 'produtos/:categoria/:productId'

so when an enter the page i add this to init

  updatePage(pageIndex:number){
    this.currentPage = pageIndex;

    this._router.navigate([], {
      relativeTo: this.route,
      queryParams: {
        page: pageIndex
      },
     queryParamsHandling: 'merge',

    });

  }

And this code above works fine eg

  • produtos/:categoria/:productId?page=1;
    produtos/:categoria/:productId?page=2;
    produtos/:categoria/:productId?page=3;

then i press backbutton

  • rodutos/:categoria/:productId?page=3;
    produtos/:categoria/:productId?page=2;

i cant leave this page for some reason, keeps reloading at same page

  • -produtos/:categoria/:productId?page=1;

Solution

  • When navigating to add query params, try adding replaceUrl: true. That should allow back button to work as if you hadn't created a new navigation event

    https://angular.io/api/router/NavigationBehaviorOptions#replaceUrl