我的代码就是这样,但不起作用。我想在路由加载之前检查用户信息。
我使用@angular/router:3.4.2
解析器类:
@Injectable()
export class UserRoleResolver implements Resolve<any> {
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): any {
let user:any = {};
user.role = 'MEMBER';
return user;
}
}
路由器:
export const APP_ROUTES: Routes = [
{path: '', component: PublicComponent, children: PUBLIC_ROUTES},
{
path: '',
component: SecureComponent,
resolve: {userRole: UserRoleResolver},
canActivate: [AuthGuard],
children: SECURE_ROUTES
}
];
安全组件:
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');
}
}
}