Search code examples
angularangular-routingangular-router

Angular Router: Navigating to previous route escapes characters


I´m currently in the work of creating a simple back-button through creating a routing-history. Every time the user navigates to a new route, I store the current url before finishing the navigation in an array such like:

  public routeHistory: string[] = [];

  ...
  if (event instanceof NavigationEnd) {
     this.routeHistory.push(event.url);
  }
  ...

Then I created a method to navigate to the previous route:

  public navigateBack(): void {
    // If there is a previous route
    if (this.routeHistory.length > 1) {
      const previousRoute: string = this.routeHistory[this.routeHistory.length - 2];

      this.router.navigate([previousRoute]).then(result => {
        // If navigation was successful
        if (result) {
          this.routeHistory.pop();
        }
      });
    }
  }

Everything works perfectly until the previousRoute contains parameters such as /route;param=value because the router.navigate method escapes '/' and '=' leading to the router navigating to /route%3Bparam%3Dvalue.

Is there a way to prevent this?


Solution

  • It should work with router.navigateByUrl(url).