【问题标题】:Dynamic Landing page based on User Role Angular Router "~3.4.0"基于用户角色 Angular 路由器“~3.4.0”的动态登陆页面
【发布时间】:2017-05-14 04:34:56
【问题描述】:

我有不同的用户角色,根据登录的用户角色,我想在登录后更改默认页面(登陆页面)。我如何实现它,在网上没有找到任何资源,是什么还需要实施,否则我遗漏了一些东西:

我有两个模块 ExceptionDashboard 和 cashierRiskprofile 模块路由

import { Routes, RouterModule } from "@angular/router";

import { CashierRiskProfileComponent }    from "./cashierriskprofile.component";

const cashierRiskProfileRoutes: Routes = [
    { path: "", redirectTo: "cashierriskprofile", pathMatch: "full" },
    { path: "cashierriskprofile", component: CashierRiskProfileComponent }
];

export const CashierRiskProfileRouting = RouterModule.forChild(cashierRiskProfileRoutes);

以上代码将默认路由为 cashierRiskprofile。

异常仪表板:

const exceptionDashboardRoutes: Routes = [
    { path: "exceptionDashboard", component: ExceptionDashboardComponent, pathMatch: 'full' },
    { path: "exceptionDetail", component: ExceptionDetailComponent, pathMatch: 'full' },
     { path: "exceptionTransaction", component: ExceptionTransactionComponent, pathMatch: 'full' }
];

对于管理员用户,我们将 CashierRiskProfile 显示为已经默认的登陆页面,对于其他用户,我想将 exceptionDashboard 路径显示为登陆页面。如何根据 UserRole 进行更改。

如果用户未通过身份验证,我知道如何实施 CanActivateViaAuthGuard“canActivate”服务以重定向到特定页面,但我正在考虑根据用户角色更改登录页面?

import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
//import { AuthService } from './auth.service';

@Injectable()
export class CanActivateViaAuthGuard implements CanActivate {

    constructor(/*private authService: AuthService*/) { }

    canActivate() {
        //if logged in return true
        return true;

        //else retrun to login page
        //return false;
    }
}

我想实现一些基于用户角色动态更改默认页面的东西。

【问题讨论】:

标签: angular angular2-routing


【解决方案1】:

在守卫内部而不是返回true 或false,您可以根据用户的角色重定向用户。

例子:

constructor(private authService: AuthService, private router: Router) {}

canActivate() {
        //if role is system manager
        if(this.checkUserRoleFunction() == "system manager"){
           this.router.navigate(['/exceptionTransaction']);
        }else if(this.checkUserRoleFunction() == "associate"){
           this.router.navigate(['/exceptionDetail']);
        }else{
           this.router.navigate(['/exceptionTransaction']);
        }
        return false;
    }

【讨论】:

    猜你喜欢
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2023-03-30
    • 2021-09-20
    • 2015-01-30
    相关资源
    最近更新 更多