gpt4 book ai didi

angular - 将空路径重定向到登录页面(Angular 4)

转载 作者:行者123 更新时间:2023-12-02 09:02:26 26 4
gpt4 key购买 nike

当用户未登录时,我想从空路径“”重定向到我的登录页面。所以我使用守卫和子路由来执行此操作。但是,我的代码适用于除“”之外的所有路线。

这是我的守卫(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);
}

这是我的 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
}

]
}

];

当我们尝试访问“htpp://localhost:4200/”时,Angular 将评估路由路径。配置的第一条路由的路径的第一部分是“”,所以让我们继续评估第二部分,它也是“”,所以我们有一个匹配!但守卫仍然需要验证用户是否可以访问 HomeLayoutComponent(如果用户已登录)。如果用户已登录,则授予访问权限,并且用户将看到 HomeLayoutComponent(导航栏以及在路由器导出中呈现的 HomeComponent),否则它将被重定向到“htpp://localhost:4200/login” ”。

假设我们正在尝试访问“htpp://localhost:4200/login”。 Angular 将再次评估路由路径。配置的第一个路由的路径的第一部分是“”,所以让我们继续评估第二部分,即“登录”,所以我们没有匹配。我们需要评估下一条配置的路由。我们再去吧!配置的第一个路由的路径的第一部分是“”(这次),所以让我们继续评估第二部分,即“login”,我们有一个匹配。在这种情况下,将显示 LoginLayoutComponent。由于 HTML 模板中只有一个 router-outlet,因此只会显示 LoginComponent。

I am using this link to create the routing

那么..我的代码有什么问题吗?

最佳答案

这对我有用

[
{
path: '',
pathMatch: 'full',
redirectTo: 'teams'
},
{
path: 'teams',
component: TeamsComponent
}
]

引用 - http://vsavkin.tumblr.com/post/146722301646/angular-router-empty-paths-componentless-routes

关于angular - 将空路径重定向到登录页面(Angular 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52394244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com