0

我的代码就是这样,但不起作用。我想在路由加载之前检查用户信息。

我使用@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');
    }
  }
}
4

1 回答 1

0

我解决了我的问题。它在安全组件中:

private route: ActivatedRoute在构造函数中添加

并打电话ngOnInıt()

this.userRole = this.route.snapshot.data['userRole'];
于 2017-02-08T08:36:02.563 回答