如何在警卫服务中获取请求网址
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]}
我需要获得成绩列表'
答案 0 :(得分:8)
在canActivate
函数中,ActivatedRouteSnapshot
和RouterStateSnapshot
作为参数传递:
@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
}
}
您应该从那里开始查看正在激活的路线