简体   繁体   English

相同的URL路径,但根据用户角色加载不同的组件-Angular

[英]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.

相关问题 Angular 2基于角色的导航在同一路径上 - Angular 2 Role based navigation on same path 角度5中的两个具有相同URL的不同组件(通过延迟加载在路由器中传递子弹) - Two Different components with same URL(passing slug in router with lazy loading) in angular 5 Angular 应用。 基于触发器(命令式或 popstate)导航到不同组件以获取路由中的相同路径 - Angular Application. Navigate to different components based on the trigger ( imperative or popstate ) for same path in routes 在Angular中使用相同的路径名为不同的base href加载不同的组件 - Load different components for different base href with same path name in Angular 相同的路径和相同数量的参数,但在Angular 5应用程序中用于路由的组件不同 - Same path and same number of parameters but different components for routing in Angular 5 application Angular - 2 个具有相同 URL 的组件 - Angular - 2 components with same URL 根据用户输入角度5+加载动态组件 - Loading dynamic components based on user input in angular 5+ Angular 同时显示不同组件加载的正确方法? - Angular right way to show loading for different components at the same time? 具有通往不同组件的相同路径的Angular路线的目的是什么? - What's the purpose of Angular routes with same path to different components? 带有枚举的 Angular 8 中基于角色的用户 - Role based user in Angular 8 with enum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM