简体   繁体   English

如何检查具有参数的匹配路由? (在路线守卫中)

[英]How to check a matching route that has params? ( in route guard)

I have a route defined which has a query param called uuid我定义了一个路由,它有一个名为 uuid 的查询参数

{path: 'is-protected/:uuid', component: protectedComponent, canActivate: [RouteGuardService]}

In the route guard, i need to check if the url matches and route param (:uuid) is empty or not.在路由守卫中,我需要检查 url 是否匹配以及路由参数 (:uuid) 是否为空。 If the route does not have the param, i need to redirect it to home route.如果路由没有参数,我需要将它重定向到主路由。 I am trying using我正在尝试使用

if (state.url == 'is-protected'){
  if( query param exists){
    return true;
  }
  else {
      this.route.navigate(['/']);
  }
}

This wont work because the state.url does not include the query param so it will never reach in this code block.这不会起作用,因为 state.url 不包含查询参数,因此它永远不会到达此代码块。

Is there a way to check if the route including params exists and navigate accordingly?有没有办法检查包含参数的路线是否存在并相应地导航?

Getting the params获取参数

You can check the route params importing ActivatedRouter from '@angular/router' in your component or guard.您可以检查从组件或守卫中的“@angular/router”导入 ActivatedRouter 的路由参数。

import { ActivatedRoute } from '@angular/router';

then inject it at contructor and subscribe the params.然后将其注入构造函数并订阅参数。

constructor( private router: ActivatedRoute ) {
    this.router.params.subscribe( (params: any) => {
      console.log('params.yourParam');
    });
   }

Then you can acces and use the param to check what you need.然后你可以访问并使用参数来检查你需要什么。

Edit:编辑:

to use throught canActivate is very similary and you can do like this:通过 canActivate 使用非常相似,您可以这样做:

(you can use ActivatedRouteSnapshot too if you dont need an observable) (如果不需要可观察对象,也可以使用 ActivatedRouteSnapshot)

import { ActivatedRouteSnapshot, CanActivate } from '@angular/router';

in you guard class get the params like this:在你的守卫课上得到这样的参数:

 canActivate( router: ActivatedRouteSnapshot ): boolean{

if ( Object.keys(router.params).length > 0 ){ 
    //then you have params

   if ( condition you need to check){
     return true
   } else { 
     return false;
   }

} else { 
     this.route.navigate(['/home']);
}

hope it helps you.希望它能帮助你。

One possible solution I implemented is create a redirection in the router itself without param and redirect it to home.我实施的一种可能的解决方案是在路由器本身中创建一个没有参数的重定向并将其重定向到家。

{ path: 'is-protected', redirectTo: 'home', pathMatch: 'full' }

This will handle if the query param doesnt exist.如果查询参数不存在,这将处理。 This method worked for me because I dont have any similar route or the route without param.这种方法对我有用,因为我没有任何类似的路线或没有参数的路线。

Now in the route guard,现在在路由守卫中,

if (state.url.indexOf('is-protected') > -1) {
    //this route will have the query param. If it didnt, it would be redirected automatically because of redirection added in list of routes
  if (condition I need to test) {
    return true
  } else {
    this.route.navigate(['/home']);
  }
}

If there is a better approach or solution, please comment.如果有更好的方法或解决方案,欢迎评论。 Thanks!谢谢!

You should inject in your constructor route: ActivatedRouteSnapshot您应该在构造函数中注入:ActivatedRouteSnapshot

You can access route parameters like this and compare them with what you expect您可以像这样访问路由参数并将它们与您期望的进行比较

let id;

if(route.paramMap.has('id')){
    assessmentId = route.paramMap.get("id");
}

I have only id params in route and for my case I used replace function for url我在路由中只有 id 参数,对于我的情况,我使用了 url 的替换函数

import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

   canActivateChild(route: ActivatedRouteSnapshot,
                    state: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean {
        const url: string = state.url.replace(/[0-9]/g, ':id');
        // if you have route like '/posts/post/5' this will return '/posts/post/:id'
        // your checking logic
   }

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

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