admin管理员组文章数量:1287517
I know that we can group routes located in one module. Like that:
canActivate: [AuthGuard],
children: [
{
path: '',
children: [
{ path: 'crises', ponent: ManageCrisesComponent },
{ path: 'heroes', ponent: ManageHeroesComponent },
{ path: '', ponent: AdminDashboardComponent }
],
}
But I should add that guard to each module's routing file. And I have many of them.
I want that the user can not go to any route except one (login route) if he is not authorized.
What is the right way to add guard to all of them??
I know that we can group routes located in one module. Like that:
canActivate: [AuthGuard],
children: [
{
path: '',
children: [
{ path: 'crises', ponent: ManageCrisesComponent },
{ path: 'heroes', ponent: ManageHeroesComponent },
{ path: '', ponent: AdminDashboardComponent }
],
}
But I should add that guard to each module's routing file. And I have many of them.
I want that the user can not go to any route except one (login route) if he is not authorized.
What is the right way to add guard to all of them??
Share Improve this question edited Nov 29, 2016 at 18:41 Sergey Tyupaev asked Nov 29, 2016 at 18:23 Sergey TyupaevSergey Tyupaev 1,27310 silver badges24 bronze badges1 Answer
Reset to default 12You can use a ponentless empty path parent route with the guard
{ path: '', canActivate: [AuthGuard], children: [
{
path: '',
children: [
{ path: 'crises', ponent: ManageCrisesComponent },
{ path: 'heroes', ponent: ManageHeroesComponent },
{ path: '', ponent: AdminDashboardComponent }
],
}
}
and in the guard check if the user is logged in.
If not logged in and the current route is login
then still allow it.
本文标签: javascriptAngular2 CanActivate guard for all routes except oneStack Overflow
版权声明:本文标题:javascript - Angular2 CanActivate guard for all routes except one - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741305283a2371330.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论