gpt4 book ai didi

angular5 - Angular 5 基于角色的路由

转载 作者:行者123 更新时间:2023-12-04 20:29:16 24 4
gpt4 key购买 nike

我有一个网站,其主页在我的路由模块中定义为:

const appRoutes: Routes = [
{
path: '',
component: HomeComponent
}];

现在我想为管理员用户显示一个不同的主页(仪表板页面)。
我可以根据用户的角色更改调用的“组件”吗?

在伪代码中类似于:
const appRoutes: Routes = [
{
path: '',
IF UserRole = 'Admin'
component: DashboardComponent
ELSE
component: HomeComponent
}];

最佳答案

@andrea06590 答案中的教程链接对基于身份验证和授权的路由进行了非常简要的概述。
简而言之,有人可以使用以下方式:

  • 使用 canActivate 属性并将其传递给实现 CanActivate 接口(interface)的服务类。
  • 使用数据对象属性将角色传递给 AuthGuard 服务。

  • app.routing.module.ts
    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    const routes: Routes = [
    { path : '' , redirectTo : '', pathMatch: 'full' , canActivate : [ RedirectGuardService ] },
    { path : 'admin' , component : AdminComponent , canActivate : [AuthGuardService] , data : { role : 'admin'}},
    { path : 'user' , component : UserComponent , canActivate : [AuthGuardService] , data : { role : 'user'}}
    { path : '**' , component : NotFoundComponent }
    ];
    @NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
    })
    export class AppRoutingModule { }

    auth-guard.service.ts
    import { Injectable } from '@angular/core';
    import { Router , CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
    import { Observable } from 'rxjs';
    import { AuthService } from '../auth-service/auth-service.service';

    @Injectable({
    providedIn: 'root'
    })
    export class AuthGuardService implements CanActivate {
    constructor(
    private router: Router,
    private authService: AuthService
    ) {}

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<boolean> | Promise<boolean> | boolean {
    const currentUser = this.authService.userVal; // Getting User Value by decoding JWT Token
    const role = route.data.role; // Getting role value which we passed to data object in router configuration
    if (currentUser) {
    if(role && role.indexOf(currentUser.role) > -1)
    return true;
    else
    return false;
    }
    return false;
    }
    }

    重定向-guard.service.ts
    import { Injectable } from '@angular/core';
    import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
    import { AuthService } from '../auth-service/auth-service.service';
    import { Observable } from 'rxjs';
    import { IUser } from 'client/app/interfaces';

    @Injectable({
    providedIn: 'root'
    })
    export class RedirectGuardService implements CanActivate {
    constructor(
    private router: Router,
    private authService: AuthService
    ) {}

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<boolean> | Promise<boolean> | boolean {
    const user = <IUser>this.authService.userVal;
    if (user && user['user']) {
    this.router.navigate(['/'+ user['user'].role]);
    return true;
    }
    this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
    return false;
    }
    }

    关于angular5 - Angular 5 基于角色的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49922264/

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