I am trying to implement canActivate for user routes, before that I want to check whether the access token is still valid or not. Therefore I implement something like this
export class AuthGuard implements CanActivate {
data:Array<Object>;
constructor(private base: BaseService){}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
var session: any = { token: localStorage.getItem('abc') };
this.base.valid(session).subscribe(result => {
this.data=result;
console.log(this.data); <-- first
});
console.log(this.data); <-- second
return true;
}
}
here I can get the data variable inside the subscribe method when I console it but outside this method is give undefined
value. How can I access it outside the method.