如何在警卫角2

时间:2017-01-03 09:02:26

标签: angular

如何在警卫服务中获取请求网址

HasPermissionService

@Injectable()
export class HasPermissionService implements CanActivate{
  private permissions = [];

  constructor(private _core:CoreService,private route1:Router, private _route:ActivatedRoute ,private route: ActivatedRouteSnapshot,private  state: RouterStateSnapshot) { 
    console.log('constructor calling ...');
    // console.log(this.route.url);
    this.permissions = this._core.getPermission();
    console.log('inside guard');
    console.log(this.permissions);
  }

  canActivate( ) {
    console.log(this.route);
    console.log(this._route);
    return true;
  }
}

但我正在变老,我来自哪里。如何获取当前网址?

路由

{path:'grade-listing',component:GradeListingComponent,canActivate:[HasPermissionService]}

我需要获得成绩列表'

1 个答案:

答案 0 :(得分:8)

canActivate函数中,ActivatedRouteSnapshotRouterStateSnapshot作为参数传递:

@Injectable()
export class HasPermissionService implements CanActivate {

   private permissions = [];

   constructor(private _core: CoreService) { 
     this.permissions = this._core.getPermission();
   } 

   canActivate(
      route: ActivatedRouteSnapshot,
      state: RouterStateSnapshot
    ): Observable<boolean>|Promise<boolean>|boolean {
       //check here
    }
}

您应该从那里开始查看正在激活的路线

CanActivate