基于Angular 6角色的根URL路由

时间:2019-01-28 10:59:23

标签: angular angular-routing angular-router angular-router-guards

我正在尝试为我的根URL实现基于角色的路由。例如,当用户登录时,我可以将他从login.component重定向到用户的仪表板页面。同样适用于管理员,也可以通过登录重定向到管理仪表板页面。但是,如果用户打开根URL,如何使用角色重定向到特定的仪表板?

当前,我的根路由指向仪表板组件,该组件解析角色并重定向到所需的仪表板页面,用户或管理员。有没有消除仪表板组件的方法?

AppRouting.ts

export const AppRoutes: Routes = [

    {
        path: '',
        redirectTo: 'dashboard',
        pathMatch: 'full'
    },  

{
        path: '',
        component: AdminLayoutComponent,
        canActivate: [AuthGuard],
        canLoad: [AuthGuard],
        children: [
            {
                path: 'dashboard',
                loadChildren: './dashboard/dashboard.module#DashboardModule'
            },  

DashboardRouting.ts

export const DashboardRoutes: Routes = [

    {
        path: '',
        component: DashboardRedirectComponent,
    },

测试仪表板重定向组件:

export class DashboardRedirectComponent implements OnInit, AfterViewInit {

    constructor(private auth: AuthenticationService, private router: Router) {

        let currentUser = JSON.parse(localStorage.getItem('currentUser'));

        if (this.auth.loggedIn()) {

            if (currentUser['role'] == 'User') {
                this.router.navigate(['/dashboard/user']);
            }

            if (currentUser['role'] == 'Admin') {
                this.router.navigate(['/dashboard/admin']);
            }
        }
    }

我尝试使用警卫甚至解析器来实现它,但是没有成功。当我打开应用程序的根页面时,它会导航到仪表板,并在几秒钟后导航到相应的仪表板页面,但是我想立即导航用户,并且对此没有额外的组件。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

如果要跳过当前路由,则需要从防护中返回false。在基于角色的重定向中,首先检查用户是否实际上是在路由到您的基本组件/dashboard而不是在特定路由/dashboard/admin,否则您将拦截基于角色的路由。然后检查角色,当您要重定向时返回false以跳过实际的路由。

例如:

 canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {

    const roles = this.permissionsService.getPermissions();

    switch (true) {
      case state.url !== '/home': {
        return true;
      }
      case !!roles['admin']: {
        this.router.navigate(['home', 'admin']);
        return false;
      }
      case !!roles['user']: {
        this.router.navigate(['home', 'user']);
        return false;
      }
      default: return true;
    }

  }

答案 1 :(得分:0)

您需要按照以下说明将Guard名称创建为 RedirectGuard

import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

@Injectable()
export class RedirectGuard implements CanActivate {
 let currentUser = null;
 let auth = null;

    constructor(private authenticationService: AuthenticationService) {
      this.currentUser = JSON.parse(localStorage.getItem('currentUser'));
      this.auth =  authenticationService;
     }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
      //Your redirection logic goes here

      if (this.auth.loggedIn()) {

        if (currentUser['role'] == 'User') {
            this.router.navigate(['/dashboard/user']);
        }

        if (currentUser['role'] == 'Admin') {
            this.router.navigate(['/dashboard/admin']);
        }
    }
   return false;

    }
}

AppRouting.ts 内部使用 RedirectGuard ,如下所示:

path: '',
        component: AdminLayoutComponent,
        canActivate: [RedirectGuard],
        canLoad: [AuthGuard],
        children: [
            {
                path: 'dashboard',
                loadChildren: './dashboard/dashboard.module#DashboardModule'
            },