简体   繁体   English

角度导航和验证 - 带参数的路线

[英]angular navigation and validation - routes with params

in my project i have some routes that accept parameters.在我的项目中,我有一些接受参数的路由。

couple of my routes (siblings):我的几条路线(兄弟姐妹):

{
  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]
    }
  ]  
},

i have a few things that i just can't figure out how to do我有一些事情我不知道该怎么做

  1. after i edit the factory (example url: 'factories/3/factor/edit'), i want to navigate back to the 'factories' url.在我编辑工厂后(示例网址:“factories/3/factor/edit”),我想导航回“工厂”网址。

i tried the following:我尝试了以下方法:

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});

all resulting in a redirect to the home page/component (url: "")所有导致重定向到主页/组件(网址:“”)

i can't navigate using a hard path (this.router.navigate(['factories']);) because i have another path that has the ability to edit a factory and i also want to return there to the parent path (from 'details/:index/factory/edit' return to 'details')我无法使用硬路径 (this.router.navigate(['factories']);) 进行导航,因为我有另一个可以编辑工厂的路径,我也想返回到父路径(从'details/:index/factory/edit' 返回到 'details')

  1. i want to validate the "index" param - check that i have a factory that match the number, and also validate that it is a number.我想验证“索引”参数 - 检查我是否有一个与数字匹配的工厂,并验证它是一个数字。 in case the validation fail redirect to the 'factories'/'details' path.如果验证失败,则重定向到“工厂”/“详细信息”路径。

i tried to that via a guard but when the user refresh the page my factories data is resetting (i use NgRX state to save the data).我试图通过警卫做到这一点,但是当用户刷新页面时,我的工厂数据正在重置(我使用 NgRX 状态来保存数据)。 i tried to use a resolver but the resolver only called after the guard, and i can't navigate from the guard or i end up in an infinite loop我尝试使用解析器,但解析器仅在警卫之后调用,我无法从警卫导航或最终陷入无限循环

any ideas how to implement that?任何想法如何实施?

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

EDIT编辑

regarding the first problem, i found a solution:关于第一个问题,我找到了一个解决方案:

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

currUrl will have an array with the full url - the first value of the array is the parent path currUrl 将有一个包含完整 url 的数组 - 数组的第一个值是父路径

still wondering about the second problem还在纠结第二个问题

you can just save the index as property in your class and navigate by that property您可以将索引保存为类中的属性并按该属性导航

export class Component implements OnInit {
   index: string | number;

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


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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM