Angular5路由:忽略canActivate之后的路由

时间:2018-04-14 16:54:05

标签: angular security routing angular5 auth-guard

我正在尝试实施路由以分隔公共区域和安全区域。

我想要完成的事情:

用户身份验证: 应该“跳过”从SecureModel加载路由的路由。

未对用户进行身份验证: 应该“跳过”从SecureModel加载路由的路由。

我的配置:

AuthGuard返回一个硬编码的假。 如果我切换到true,则所有安全路由都按预期工作。

APP-routing.module.ts

const routes: Routes = [
{
    path: '',
    canActivate: [ AuthGuard ],
    loadChildren: 'app/secured/secured.module#SecuredModule'
},
{
    path: '',
    loadChildren: 'app/public/public.module#PublicModule'
},
{ path: '**', redirectTo: '' }
];

固定-routing.module.ts

const routes: Routes = [
{
    path: '',
    component: SecuredLayoutComponent,
    children: [
    { path: '', redirectTo: 'customer-orders', pathMatch: 'full' },
    {
        path: 'customer-orders',
        loadChildren: 'app/secured/customer-orders/customer-orders.module#CustomerOrdersModule'
    },
    {
        path: 'manufacturers',
        loadChildren: 'app/secured/manufacturers/manufacturers.module#ManufacturersModule'
    },
    {
        path: 'roles',
        loadChildren: 'app/secured/roles/roles.module#RolesModule'
    },
    {
        path: 'users',
        loadChildren: 'app/secured/users/users.module#UsersModule'
    },
    { path: '**', component: Error404Component }
    ]
}
];

公共routing.module.ts

const routes: Routes = [
{
    path: '',
    component: PublicLayoutComponent,
    children: [
    { path: '', redirectTo: 'login', pathMatch: 'full' },
    { path: 'login', component: LoginComponent },
    { path: '**', component: Error404Component }
    ]
}
];

当我评论其中一条主要路线时,他们自己的公共和安全路线正在工作。

我非常喜欢这种路由结构,如果有人可以帮助公共路径和安全路由之间的“切换”工作,那将会很棒。

1 个答案:

答案 0 :(得分:0)

您不能为路径设置空白。尝试下面的一个:

const routes: Routes = [
{
    path: 'a',
    canActivate: [ AuthGuard ],
    loadChildren: 'app/secured/secured.module#SecuredModule'
},
{
    path: '',
    loadChildren: 'app/public/public.module#PublicModule'
},
{ path: '**', redirectTo: '' }
];
相关问题