在带有路由器模块 v 3.0.0.6alpha 的 angular2 rc2 中,我扩展了 RouterOulet 以在访问管理员之前检查用户是否已登录。所以这是代码:
@Directive({
selector: 'router-outlet'
})
export class LoggedInRouterOutlet extends RouterOutlet
{
publicRoutes: Array<string>;
private parentOutletMap: RouterOutletMap;
private userService: UserService;
private parentRouter: Router;
constructor(
parentOutletMap: RouterOutletMap,
_location: ViewContainerRef,
@Attribute('name') name: string,
userService: UserService,
parentRouter: Router
) {
super(parentOutletMap, _location, name);
this.parentRouter = parentRouter;
this.parentOutletMap = parentOutletMap;
this.userService = userService;
this.publicRoutes = [
'public',
'login'
];
}
activate(factory: ComponentFactory<any>, activatedRoute: ActivatedRoute, providers: ResolvedReflectiveProvider[], outletMap: RouterOutletMap)
{
if (this._canActivate(factory.selector)) {
return super.activate(factory, activatedRoute, providers, outletMap);
}
this.parentRouter.navigate(['/login']);
}
_canActivate(url) {
return this.publicRoutes.indexOf(url) !== -1 || this.userService.isLoggedIn()
}
}
userService.isLoggedIn() 必须返回一个布尔值。我的问题是:如何调整我的代码以进行 http 调用以检查用户是否已登录?因为如果 isLoggedIn 方法返回一个 observable 对象,并且我订阅了它,我无法在父函数中返回结果。