代码之家  ›  专栏  ›  技术社区  ›  Alexander Yakovlev

刷新当前路由会重定向到父路由,而不是在5中

  •  2
  • Alexander Yakovlev  · 技术社区  · 7 年前

    我有5个角路由配置如下:

      {path: 'malls', component: MallsComponent},
      {
        path: 'malls/new', component: MallEditComponent,
        resolve: {body: MallResolve}
      },
      {
        path: 'malls/:id', component: MallEditComponent,
        resolve: {body: MallResolve}
      },
      {
        path: 'malls/:mallId/point-sales', component: MallPointSalesComponent,
      }
    

    private changeFilterRoute() {
      let queryParams = {
        'sorter.sortOrder': this.sort.active,
        'sorter.direction': this.sort.direction,
        'page': this.paginator.pageIndex,
        'pageSize': this.paginator.pageSize
      };
    
      this.router.navigate(this.route.snapshot.url, {
        queryParams: queryParams
      })
    }
    

    方法 每次更改分页或筛选参数时调用,并应重新加载当前页。但它并没有重新加载当前页面,而是将用户重定向到父路径。

    比如我在路上

    商场/{:mallId}/点销售

    mallId只是一个数字。然后我更改任何分页/过滤参数 changeFilterRoute() 调用时,该方法意外地将我重定向到其父路由:

    具有 处理。

    2 回复  |  直到 7 年前
        1
  •  4
  •   Konstantin Devyatkin    7 年前

    问题在于调用 router.navigate 您应该从urlSegments中提取路径字符串 route.snapshot.url

    this.router.navigate(this.route.snapshot.url.map(urlSegment => urlSegment.path), {
        queryParams: queryParams
    });
    
        2
  •  0
  •   Fateh Mohamed    7 年前

    试试这个

     constructor(public router: Router, private route: ActivatedRoute,) {}
     ...
    
     this.router.navigate(['.'], {queryParams: queryParams,  relativeTo: this.route});
    
    推荐文章