【发布时间】:2018-06-05 18:21:45
【问题描述】:
我的应用中有两个模块:HomeModule 和 LoginModule。
我希望能够在同一路径中加载两个不同的模块。加载哪个模块取决于用户是否登录。
如果用户已登录,HomeModule 应该为他加载,如果没有 - LoginModule 应该加载。
我尝试了这段代码,控制台没有错误,HomeModule 如果用户登录则加载,但如果用户未登录 - 没有加载。如果我颠倒路线的顺序,LoginModule 有效,而 HomeModule 无效。
HomeGuard.canLoad() 返回如果用户已登录,则为 true,如果用户未登录,LoginGuard.canLoad() 返回 true。
HomeGuard.canLoad:
if (this.userSessionService.userSession) {
return true;
}
this.router.navigate(['./']);
return false;
LoginGuard.canLoad:
if (this.userSessionService.userSession) {
this.router.navigate(['./']);
return false;
}
return true;
我的路线:
{ path: '', loadChildren: './home/index#HomeModule', canLoad: [HomeGuard] },
{ path: '', loadChildren: './login/index#LoginModule', canLoad: [LoginGuard]}
它应该像 Facebook 网络应用程序中的主路由一样工作。如果我去 facebook.com 如果我没有登录,我会看到登录页面,如果我登录,我会看到应用程序作为登录用户。
【问题讨论】: