0

I assure you, this question is not a duplicate. Please read the question first before discarding it.

First look at the code then I'll explain.

  constructor(private router: Router, private activatedRoute: ActivatedRoute) {
  }
  private someFunction(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if (route.queryParams.hasOwnProperty('rew')) {
      const url = new URL((new URL(window.location.href)).origin + state.url.toString());
      url.searchParams.delete('rew');
      let queryParams: Record<string, string> = {};
      url.searchParams.forEach((value, key) => {
        queryParams[key] = value;
      });
      this.router.navigate(['.'], { relativeTo: this.activatedRoute, queryParams: queryParams }).catch(console.log);
    }
    return true;
  }

As you can see I have a function, in which I'm checking if the queryParams contain 'rew' and then remove it. Then redirect to the same page with the updated queryParams.

But redirection is not working.

How to redirect to the same page with updated queryParams? Remember the page linking is dynamic, so static links like this this.route.navigate('/contact') won't work.

  • Does this answer your question? [Angular2 router - Navigate to the current page with different parameters](https://stackoverflow.com/questions/39613093/angular2-router-navigate-to-the-current-page-with-different-parameters) – Harun Yilmaz Sep 26 '22 at 10:22

1 Answers1

-1
  private someFunction(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    const { rew, ...newParams } = route.queryParams;
    if (rew) return this.router
      .navigateByUrl('.', { relativeTo: this.route, queryParams: newParams })
      .then(() => false);
    return true;
  }

First, too much code for something this simple.

Next, you have to PREVENT the guard from resolving to route away. Otherwise, the guards validates, then routes to the expected route.

MGX
  • 2,534
  • 2
  • 14