Mvr*_*ram 4 guard angular-routing angular
当用户未登录时,我想从空路径“”重定向到我的登录页面。所以我使用警卫和儿童路线来做到这一点。但是,我的代码适用于除“ ”之外的所有路线。
这是我的守卫(canValidate 函数)
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);
}
Run Code Online (Sandbox Code Playgroud)
这是我的 app-routing.module.ts(仅路由数组)
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
}
]
}
];
Run Code Online (Sandbox Code Playgroud)
当我们尝试访问“htpp://localhost:4200/”时,Angular 将评估路由路径。配置的第一个路由的路径的第一部分是“”,所以让我们继续评估也是“”的第二部分,所以我们有一个匹配!但是守卫仍然需要验证用户是否可以访问 HomeLayoutComponent(如果用户已登录)。如果用户已登录,则授予访问权限,用户将看到 HomeLayoutComponent(导航栏以及在路由器插座中呈现的 HomeComponent),否则将重定向到“htpp://localhost:4200/login ”。
所以假设我们试图访问“htpp://localhost:4200/login”。Angular 将再次评估路由路径。配置的第一个路由的路径的第一部分是“”,所以让我们继续评估“登录”的第二部分,所以我们没有匹配。我们需要评估下一个配置的路由。让我们再去一次!配置的第一个路由的路径的第一部分是“”(这次),所以让我们继续评估“登录”的第二部分,我们有一个匹配。在这种情况下,将显示 LoginLayoutComponent。由于 HTML 模板中只有一个路由器出口,因此只会显示 LoginComponent。
那么..我的代码有什么问题?
这对我有用
[
{
path: '',
pathMatch: 'full',
redirectTo: 'teams'
},
{
path: 'teams',
component: TeamsComponent
}
]
Run Code Online (Sandbox Code Playgroud)
参考 - http://vsavkin.tumblr.com/post/146722301646/angular-router-empty-paths-componentless-routes
我设法解决了我的问题,包括没有警卫重定向到登录页面的空路由,这样,如果该人未登录,他将重定向他登录,否则,他将输入以下定义,始终确保该人已经这个已登录系统。
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],
},
];
Run Code Online (Sandbox Code Playgroud)
PS:对不起我的英语