i want to redirect from the empty path " " to my login page, when user is not logedd in. So i'm using guards and children routes to do this. However, my code is fine to all routes except " ".
This is my guard (canValidate function)
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):boolean {
return this.canMethod();
}
canMethod () {
if (Environment.ActivateGuard) {
let token = this._auth.getCookie(AuthorizationEnum.cookieName);
if(token) { // token exists
let data = {
token: token,
module: this.module
};
this._auth.validate(data, AuthorizationEnum.Bearer).subscribe(
data => { // valid token
if ( data.res == AuthorizationEnum.emptyResponse) { // invalid user, it doesn't belongs to this module.
this.letsRedirect();
}
else {
let user_role = data.det[1]; // this is the user's role!.
}
},
error => { // other error
this.letsRedirect();
}
);
return true;
}
else { // user don't have token
this.letsRedirect();
}
}
else {
return true;
}
}
letsRedirect(){
window.location.href = Environment.authAppUrl + AuthorizationEnum.redirectQuery + encodeURIComponent(this.url);
}
And this is my app-routing.module.ts(only the array of routes)
const routes: Routes = [
// TODO: Validar esta ruta
//{ path: '', component: LoginComponent, pathMatch: 'full' },
//Con header
{
path: '',
children: [
{ path: '', component: HomeComponent,canActivate:[AuthorizatedGuard]},
{ path: 'inicio', component: HomeComponent, canActivate:[AuthorizatedGuard] },
{ path: 'categorias', redirectTo: 'base-lista/categorias'},
{ path: 'videos', redirectTo: 'base-lista/videos'},
{ path: 'base-lista/:idList', component: BaseListComponent, canActivate:[AuthorizatedGuard] },
{ path: 'base-lista/categorias/gestionar-categorias', component: CategoryVideoComponent, canActivate:[AuthorizatedGuard] },
{ path: 'base-lista/categorias/ver-detalles-categoria', component: ViewDetailsCategoryComponent, canActivate:[AuthorizatedGuard] },
{ path: 'base-lista/videos/gestionar-videos', component: VideoComponent, canActivate:[AuthorizatedGuard] },
{ path: 'base-lista/videos/ver-detalles-video', component: ViewDetailsVideoComponent, canActivate:[AuthorizatedGuard] },
],
component: AppLayoutComponent,
canActivate: [AuthorizatedGuard],
},
//sin header
{
path: '',
component: LoginComponent,
children: [
{
path: 'login',
component: LoginComponent
}
]
}
];
When we try to access "htpp://localhost:4200/", Angular will evaluate the route paths. The first part of the path for the first route configured is "", so let’s continue evaluating the second part which is also "", so we have a match! But the guard still need to verify if the user can access the HomeLayoutComponent (if user is logged in). If the user is logged in, then the access is granted and the user will see the HomeLayoutComponent (the navbar along with the HomeComponent being rendered in the router-outlet), otherwise it is redirected to "htpp://localhost:4200/login".
So suppose we are trying to access "htpp://localhost:4200/login". Angular will evaluate the route path again. The first part of the path for the first route configured is "", so let’s continue evaluating the second part which is "login", so we do not have a match with. We need to evaluate the next route configured. Let’s go again! The first part of the path for the first route configured is "" (this time), so let’s continue evaluating the second part which is "login" and we have a match . In this case the LoginLayoutComponent is displayed. Since there is only a router-outlet in the HTML template, only the LoginComponent will be displayed.
I am using this link to create the routing
So.. what is wrong with my code?
答案 0 :(得分:1)
您是否尝试过从''
删除LoginComponent
路径?似乎多余。除非您使用单独的布局,否则您需要指定要使用的布局组件(例如AppLayoutCompnent
),而不要两次指定LoginComponent
。
更改
{
path: '',
component: LoginComponent,
children: [
{
path: 'login',
component: LoginComponent
}
]
}
只是
{
path: 'login',
component: LoginComponent
}
OR
{
path: '',
component: LoginLayoutComponent, // <---
children: [
{
path: 'login',
component: LoginComponent
}
]
}
您应该查看的另一件事是使用路由器而不是window.location
处理重定向。这是处理它的“角度”方式。将Router
注入您的守卫中,并使用它进行导航。
letsRedirect(){
this.router.navigate(['/login'], { queryParams: { redirectUrl: this.url } });
}
最后,您可以使用CanActivateChild
简化路线。它将对所有子路由执行检查,而不是对每个子路由都添加防护。
{
path: '',
children: [
{ path: '', component: HomeComponent,
{ path: 'inicio', component: HomeComponent },
{ path: 'categorias', redirectTo: 'base-lista/categorias'},
{ path: 'videos', redirectTo: 'base-lista/videos'},
{ path: 'base-lista/:idList', component: BaseListComponent },
{ path: 'base-lista/categorias/gestionar-categorias', component: CategoryVideoComponent },
{ path: 'base-lista/categorias/ver-detalles-categoria', component: ViewDetailsCategoryComponent },
{ path: 'base-lista/videos/gestionar-videos', component: VideoComponent },
{ path: 'base-lista/videos/ver-detalles-video', component: ViewDetailsVideoComponent },
],
component: AppLayoutComponent,
canActivateChild: [AuthorizatedGuard],
},
并将警卫更新为
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):boolean
答案 1 :(得分:1)
我设法解决了我的问题,包括空路径,而没有保护措施重定向到登录页面,因此,如果该人未登录,他将重定向他以登录,否则他将输入以下定义,以确保已登录系统的人。
const routes: Routes = [
// Sin header
{ path: '', component: LoginComponent, pathMatch: 'full' },
//Con header
{
path: '',
children: [
{ path: '', component: HomeComponent,canActivate:[AuthorizatedGuard]},
{ path: 'inicio', component: HomeComponent, canActivate:[AuthorizatedGuard] },
{ path: 'categorias', redirectTo: 'base-lista/categorias'},
{ path: 'videos', redirectTo: 'base-lista/videos'},
{ path: 'base-lista/:idList', component: BaseListComponent, canActivate:[AuthorizatedGuard] },
{ path: 'base-lista/categorias/gestionar-categorias', component: CategoryVideoComponent, canActivate:[AuthorizatedGuard] },
{ path: 'base-lista/categorias/ver-detalles-categoria', component: ViewDetailsCategoryComponent, canActivate:[AuthorizatedGuard] },
{ path: 'base-lista/videos/gestionar-videos', component: VideoComponent, canActivate:[AuthorizatedGuard] },
{ path: 'base-lista/videos/ver-detalles-video', component: ViewDetailsVideoComponent, canActivate:[AuthorizatedGuard] },
],
component: AppLayoutComponent,
canActivate: [AuthorizatedGuard],
},
];
ps:对不起,我的英语
答案 2 :(得分:1)
这对我有用
[
{
path: '',
pathMatch: 'full',
redirectTo: 'teams'
},
{
path: 'teams',
component: TeamsComponent
}
]
参考-http://vsavkin.tumblr.com/post/146722301646/angular-router-empty-paths-componentless-routes