在我的项目中,我有一些接受参数的路由。
我的几条路线(兄弟姐妹):
{
path: '', component: HomeComponent,
},
{
path: 'factories', component: FactoriesComponent, canActivate: [AuthGuard],
resolve: [FactoriesResolverService],
children: [
{
path: ':index/factory', component: DetailsFactoryComponent, canActivate: [DetailsFactoryGuard]
},
{
path: ':index/factory/edit', component: EditFactoryComponent, canActivate: [DetailsFactoryGuard]
}
]
},我有几件事我不知道该怎么做
在编辑工厂(例如url:‘/3/factor/
我尝试了以下几点:
private router: Router
private route: ActivatedRoute
this.router.navigate(['../']);
this.router.navigate(['../'], {relativeTo: this.route});
this.router.navigate(['../../'], {relativeTo: this.route});
this.router.navigate(['../..'], {relativeTo: this.route});
this.router.navigate([''], {relativeTo: this.route});
this.router.navigate(['/'], {relativeTo: this.route});
this.router.navigate(['../'], {relativeTo: this.route.parent});所有这些都会导致重定向到主页/组件(url:"")
我无法使用硬路径(this.router.navigate(“工厂”);)导航,因为我有另一条能够编辑工厂的路径,并且我还想返回到父路径(从‘details/:index/ factory /编辑’return 'details')。
我试图通过一个保护程序这样做,但是当用户刷新页面时,我的工厂数据正在重置(我使用NgRX状态保存数据)。我试着使用解析器,但是解析器只在保护之后调用,我无法从保护器中导航,否则我会陷入无限循环。
有什么办法可以实现吗?
=======================
编辑
关于第一个问题,我找到了一个解决办法:
const currUrl = this.route.snapshot'_routerState'.url.substring(1).split("/");this.router.navigate([currUrl]);
currUrl将有一个带有完整url的数组-数组的第一个值是父路径。
还在想第二个问题
发布于 2020-01-13 22:52:14
只需将索引保存为类中的属性,并按该属性导航即可。
export class Component implements OnInit {
index: string | number;
ngOnInit() {
this.index = this.route.snapshot.params['index'];
}
onNavigate() {
this.router.navigate(['/', this.index,'factory' ]);
}
}https://stackoverflow.com/questions/59725060
复制相似问题