【问题标题】:Angular2 authguard routesAngular2 authguard 路由
【发布时间】:2017-05-16 16:18:15
【问题描述】:

我正在开发一个应用程序网络,现在我遇到了路由问题。 我创建了一个 AuthGuard,如果有用户登录则返回。好吧,当我想分成两种类型的路线时,问题就开始了:

-如果我没有登录,我只能去的路线。 -只有登录后才能去的路线。

所以如果我登录了我想要的是我不能去一些路线。

这是我的路由器:

{ path: 'app',
component: AppComponent,
children: [
  { path: 'login', component: LoginComponent },
  { path: 'inicio', component: ContentHomeComponent },
  { path: 'register', component: RegisterFormComponent},
  { path: 'apuestas', component: ContentBetComponent, canActivate: [AuthGuard]}
]},
  { path: '**', redirectTo:'/app/inicio',pathMatch: 'full' }

我现在拥有的是,如果我登录了,我只能去“apuestas”,但我想将我所说的分成两部分。我希望只有在您未登录时才能访问 login、inicio 和 register,如果您已注册,则可以访问其他。

【问题讨论】:

    标签: angular


    【解决方案1】:

    如果我理解正确,您可以添加另一个 NotAuthenticatedGuard,如果用户已登录,则在 canActivate 上返回 false

    { path: 'app',
      component: AppComponent,
      children: [
        { path: 'login', component: LoginComponent, canActivate: [ NotAuthGuard ] },
        { path: 'inicio', component: ContentHomeComponent, canActivate: [ NotAuthGuard ] },
        { path: 'register', component: RegisterFormComponent, canActivate: [ NotAuthGuard ] },
        { path: 'apuestas', component: ContentBetComponent, canActivate: [AuthGuard]}
    ]},
    { path: '**', redirectTo:'/app/inicio',pathMatch: 'full' }
    

    命名当然由你决定。

    【讨论】:

    • 但是没有更好的方法吗?例如使用父母和孩子或类似的东西。我不知道是否创建另一个服务来检查它,它是有效的
    猜你喜欢
    • 2017-06-09
    • 2017-06-25
    • 2016-11-19
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    相关资源
    最近更新 更多