Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用查询参数和片段实现路由相对重定向_Javascript_Angular_Angular Routing_Angular Route Segment - Fatal编程技术网

Javascript 如何使用查询参数和片段实现路由相对重定向

Javascript 如何使用查询参数和片段实现路由相对重定向,javascript,angular,angular-routing,angular-route-segment,Javascript,Angular,Angular Routing,Angular Route Segment,我在开发角度项目时有一个问题 我试图找到解决方案,如何实现路由相对重定向与 当URL重定向时,其中origin重定向有其相对的查询参数和片段。 例如,在Angular.io的指南中 const heroesRoutes: Routes = [ { path: 'heroes', redirectTo: '/superheroes' }, { path: 'hero/:id', redirectTo: '/superhero/:id' }, { path: 'superheroes',

我在开发角度项目时有一个问题

我试图找到解决方案,如何实现路由相对重定向与 当URL重定向时,其中origin重定向有其相对的查询参数和
片段。

例如,在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详细信息中,
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});
}

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

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

this.router.navigate(['../', { id: heroId, foo: 'foo' },  {relativeTo: this.route}]);

你可以用像-

 this.router.navigate(
    ['hero','12345'],
    {
      relativeTo: this.route,
      queryParams: {
        type: 'value',
      },
    }
  );

@pixelbilts,谢谢你的回复。但它不起作用。请检查本指南样本<代码>/heros重定向到/superheros。英雄列表组件不是英雄详细信息组件的父级。