2

我在开发 Angular 项目时遇到了一个问题。

我试图找到有关如何使用当 URL 重定向时实现路由相对重定向的解决方案,其中原始重定向具有其相对的查询参数和fragment.

例如,在 Angular.io 的指南中。

const heroesRoutes: Routes = [
  { path: 'heroes', redirectTo: '/superheroes' },
  { path: 'hero/:id', redirectTo: '/superhero/:id' },
  { path: 'superheroes',  component: HeroListComponent },
  { path: 'superhero/:id', component: HeroDetailComponent }
];

在 hero-detail 中,goback()用于导航回 HeroListComponent 的函数。我补充说relativeTo: this.route。但它不起作用。

gotoHeroes(hero: Hero) {
  let heroId = hero ? hero.id : null;
  // Pass along the hero id if available
  // so that the HeroList component can select that hero.
  // Include a junk 'foo' property for fun.
  this.router.navigate(['/heroes', { id: heroId, foo: 'foo' }], {relativeTo: this.route});
}

在配置重定向到超级英雄时,我不知道如何实现这个功能。

4

2 回答 2

1

您需要一条相对路线,因此根据您所在的位置,这应该可以返回:

this.router.navigate(['../', { id: heroId, foo: 'foo' },  {relativeTo: this.route}]);
于 2017-12-13T06:36:29.723 回答
0

你可以使用类似的东西 -

 this.router.navigate(
    ['hero','12345'],
    {
      relativeTo: this.route,
      queryParams: {
        type: 'value',
      },
    }
  );
于 2021-06-07T17:30:05.290 回答