My codes like that but don't working. I want to check user info before route loading.
I use @angular/router:3.4.2
Resolver Class:
@Injectable()
export class UserRoleResolver implements Resolve<any> {
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): any {
let user:any = {};
user.role = 'MEMBER';
return user;
}
}
Router:
export const APP_ROUTES: Routes = [
{path: '', component: PublicComponent, children: PUBLIC_ROUTES},
{
path: '',
component: SecureComponent,
resolve: {userRole: UserRoleResolver},
canActivate: [AuthGuard],
children: SECURE_ROUTES
}
];
Secure Component:
export class SecureComponent implements OnInit {
userRole:any;
role:string = 'MEMBER';
constructor(private userService: UserService,private router:Router) {
}
ngOnInit() {
this.role = 'MEMBER';
console.log('Role',this.userRole);
if (!this.userService.isLoggedIn()) {
this.router.navigateByUrl('/login');
}
}
}
İ solve my problem. It's in secure component:
I add
private route: ActivatedRoute
in constructorAnd call in
ngOnInıt()