我在开发 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});
}
在配置重定向到超级英雄时,我不知道如何实现这个功能。