[英]Same URL path but loading different components based on User role - Angular
I want to have 2 components with the same route (root '/'), but loading just one based on the User's role (Guest or Authenticated). 我想让2个组件具有相同的路由(根“ /”),但仅根据用户角色(来宾或已验证)加载一个组件。 So, a home page for Guest and a home page for Authenticated with the same path. 因此,Guest的主页和Authenticated的主页具有相同的路径。
I tried creating a guard for Guest and a guard for Authenticated users and apply each in its component in the Routes array, like this: 我尝试为Guest创建一个防护,为Authenticated用户创建一个防护,并在Routes数组的组件中应用每个防护,如下所示:
export const AppRoutes: Routes = [
{
path: '',
component: GuestLayoutComponent,
canActivate: [GuestGuard],
children: [
{
path: '',
loadChildren: './guest-components/guest.module#GuestModule',
}
],
},
{
path: '',
component: AuthLayoutComponent,
canActivate: [AuthGuard],
children: [
{
path: '',
loadChildren: './auth-components/auth.module#AuthModule',
}
],
},
{
path: '**',
component: ErrComponent,
},
];
But it created a infinite loop. 但是它创建了一个无限循环。
Also sharing: 还分享:
GuestRoutes : GuestRoutes :
export const GuestRoutes: Routes = [
{
path: '',
children: [
{
path: '',
component: GuestHomeComponent,
}, {
path: 'about-guest',
component: GuestAboutComponent,
pathMatch: 'full'
},
]
}
];
AuthRoutes: AuthRoutes:
export const AuthRoutes: Routes = [
{
path: '',
children: [
{
path: '',
component: AuthHomeComponent,
}, {
path: 'about',
component: AuthAboutComponent,
pathMatch: 'full'
},
]
}
AuthGuard: AuthGuard:
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authService.isAuthenticated()
.then(
(authenticated: boolean) => {
if (authenticated) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
}
);
}
}
GuestGuard: GuestGuard:
export class GuestGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authService.isAuthenticated()
.then(
(authenticated: boolean) => {
if (authenticated) {
this.router.navigate(['/']);
return false;
} else {
return true;
}
}
);
}
}
Please find the link to the demo . 请找到演示的链接 。
How can I achieve this? 我该如何实现? I would appreciate any help. 我将不胜感激任何帮助。
I don't think it is possible to have 2 components with the exact same route. 我认为不可能有2个组件具有完全相同的路线。 I can't remember having seen a solution for that before. 我不记得以前曾见过解决方案。
I don't know exactly why you want to accomplish this, but hopefully the following approach could help you. 我不知道您为什么要完成此任务,但是希望以下方法可以对您有所帮助。 Create a parent component called ContainerHomeComponent that you point your route towards. 创建一个称为ContainerHomeComponent的父组件,将其指向您的路线。 In the .ts file you query the "this.authService.isAuthenticated()" to find if the user is authenticated and assign to a propery called "authenticated". 在.ts文件中,您查询“ this.authService.isAuthenticated()”以查找用户是否已通过身份验证,并分配给名为“ authenticated”的属性。 In the template all you have is: 在模板中,您所拥有的是:
<app-guest-home-component *ngIf="!authenticated"></app-guest-home-component>
<app-auth-home-component *ngIf="authenticated"></app-auth-home-component>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.