36

我的项目中有一个 AccessGuard 类,它的工作是确定用户是否可以访问该路由。我使用router.url来获取当前路线,但 url 在导航到新路线之前返回路线,就像我在用户路线中一样,我单击候选路线,因此 url 返回用户而不是我希望验证访问的候选者到路线这是我的路线文件:

const routes:Routes = [
{
    path:'',
    component:PanelComponent,
    canActivate:[AuthGuard,AccessGuard],
    canActivateChild:[AuthGuard,AccessGuard],
    children:[
        {
            path:'dashboard',
            component:DashboardComponent
        },
        {
            path:'users',
            component:UsersComponent
        },
        {
            path:'users/:id',
            component:ShowUserComponent
        },
        {
            path:'candidates',
            component:CandidatesComponent
        },
        {
            path:'permissions',
            component:PermissionsComponent
        },
        {
            path:'holidays',
            component:HolidaysComponent
        },
        {
            path:'candidate/:id',
            component:CandidateComponent
        },
        {
            path:'salary/create',
            component:InsertSalaryComponent
        },
        {
            path:'document/create',
            component:InsertDocumentComponent
        },
        {
            path:'leave/create',
            component:InsertLeaveComponent
        }
    ]
}

];

这是我的门禁:

permissions;
currentRoute;
constructor(private authService:AuthService,private router:Router){
    this.permissions = this.authService.getPermissions();
}

canActivate(){
    return this.checkHavePermission();
}

canActivateChild(){
    console.log(this.router.url);
    return this.checkHavePermission();
}

private checkHavePermission(){
    switch (this.router.url) {
        case "/panel/users":
            return this.getPermission('user.view');
        case '/panel/dashboard':
            return true;
        case '/panel/candidates':
            return this.getPermission('candidate.view');
        default:
            return true;
    }
}


getPermission(perm){
    for(var i=0;i<this.permissions.length;i++){
        if(this.permissions[i].name == perm ){
            return true;
        }
    }
    return false;
}
4

3 回答 3

62

您需要使用方法参数来查看目标路由:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
  console.log(state.url);//'candidates'
}

canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot)
于 2016-09-28T12:24:51.717 回答
5

这可以帮助你:

  1. 导入ActivatedRouteSnapshotRouterStateSnapshot

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

  2. canActivate中的签名:

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable {}

  3. 检查状态

    console.log("route-access",state);

你的 Guard 文件看起来像这样:

    import { Injectable } from '@angular/core';
    import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
    import { Observable } from 'rxjs';
    import { AutenticacionService } from 'app/services/index';
    @Injectable()
    export class AuthGuard implements CanActivate {
        constructor(private _router: Router, private auth:AutenticacionService) {}
    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> {
        // console.log("route-access",state);
        let _url:string="";
        state.url.split("/").forEach(element => {
            if(_url==="")
                if(element!=="")
                    _url=element;
        });
        // console.log(_url);
        return this.auth.check(_url)
          .map((result) => {
                    if (result) {
                        return true;
                    } else {
                        this._router.navigate(['/']);
                        return false;
                    }
                });  
    }

}
于 2017-05-01T15:45:25.427 回答
0

当前 URL 可以从 RouterStateSnapshot 返回,如下所示,

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

    canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot){
      console.log(state.url);  
      ...
    }
于 2018-11-11T08:16:57.273 回答