简体   繁体   中英

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?

It should work with router.navigateByUrl(url) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM