小编Max*_*ink的帖子

Angular 2不同组件具有相同路线

我有一个应用程序,它需要将经过身份验证的用户和来宾用户的组件分开。但是我需要,两个组件都将通过“ /”路由加载。我写

{
    path: 'desktop',
    loadChildren: 'app/member/member.module#MemberModule',
    canActivate: [LoggedInGuard],
},
{
    path: '',
    loadChildren: 'app/guest/guest.module#GuestModule',
    canActivate: [GuestGuard],
},
Run Code Online (Sandbox Code Playgroud)

而且有效。但是,如何使两个组件都通过相同的网址加载?我曾尝试path: ''为会员的模块路由编写程序,但是未执行第二个路由器规则。这是警卫代码:

LoggedInGuard:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if(this.sessionService.isLoggedIn()) {
        return true;
    } else {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

GuestGuard:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if(!this.sessionService.isLoggedIn()) {
        return true;
    } else {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个小矮人:http ://embed.plnkr.co/VaiibEVGE79QU8toWSg6/

我应该如何正确做?谢谢

angular2-routing angular

7
推荐指数
3
解决办法
3817
查看次数

标签 统计

angular ×1

angular2-routing ×1