首页 > 解决方案 > Angular Router:导航到上一个路由转义字符

问题描述

我目前正在通过创建路由历史来创建一个简单的后退按钮。每次用户导航到新路线时,我都会在完成导航之前将当前 url 存储在一个数组中,例如:

  public routeHistory: string[] = [];

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

然后我创建了一个导航到上一条路线的方法:

  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();
        }
      });
    }
  }

一切正常,直到 previousRoute 包含参数,例如/route;param=value因为该router.navigate方法转义 '/' 和 '=' 导致路由器导航到/route%3Bparam%3Dvalue.

有没有办法防止这种情况?

标签: angularangular-routingangular-router

解决方案


它应该与router.navigateByUrl(url).


推荐阅读