首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角导航和验证.带傍角的路径

角导航和验证.带傍角的路径
EN

Stack Overflow用户
提问于 2020-01-13 22:25:13
回答 1查看 778关注 0票数 1

在我的项目中,我有一些接受参数的路由。

我的几条路线(兄弟姐妹):

代码语言:javascript
复制
{
  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/

  1. ’)之后,我想导航回工厂url.

我尝试了以下几点:

代码语言:javascript
复制
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')。

  1. ,我想验证“索引”参数-检查我有一个与数字匹配的工厂,并验证它是一个数字。如果验证失败,请重定向到“工厂”/“详细信息”路径.

我试图通过一个保护程序这样做,但是当用户刷新页面时,我的工厂数据正在重置(我使用NgRX状态保存数据)。我试着使用解析器,但是解析器只在保护之后调用,我无法从保护器中导航,否则我会陷入无限循环。

有什么办法可以实现吗?

=======================

编辑

关于第一个问题,我找到了一个解决办法:

const currUrl = this.route.snapshot'_routerState'.url.substring(1).split("/");this.router.navigate([currUrl]);

currUrl将有一个带有完整url的数组-数组的第一个值是父路径。

还在想第二个问题

EN

回答 1

Stack Overflow用户

发布于 2020-01-13 22:52:14

只需将索引保存为类中的属性,并按该属性导航即可。

代码语言:javascript
复制
export class Component implements OnInit {
   index: string | number;

   ngOnInit() {
     this.index = this.route.snapshot.params['index'];
   }


   onNavigate() {
      this.router.navigate(['/', this.index,'factory' ]);
   }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59725060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档