angular - 基于用户角色的动态登陆页面 Angular Router "~3.4.0"

标签 angular angular2-routing

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

我有两个模块 ExceptionDashboard 和 cashierRiskprofile 模块 routes

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”服务以在用户未经过身份验证时重定向到特定页面,但我正在考虑根据用户 Angular 色更改登录页面?

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 色动态更改默认页面的东西。

最佳答案

在守卫内部,您可以根据用户的 Angular 色重定向用户,而不是返回 truefalse

例子:

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;
    }

关于angular - 基于用户角色的动态登陆页面 Angular Router "~3.4.0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41386684/

相关文章:

angular - 在angular2中插入具有相对路径的图像

相互依赖的 Angular 库辅助入口点

arrays - Angular2 将嵌套的 json 数组映射到模型

angular - 缺少必需的获取参数 Angular 4

Angular 、延迟加载、模块间依赖、最佳实践

javascript - 带参数的 Angular 'lazy' 路由在本地有效,但在生产中无效

angular - 找不到名称@Routes

在组件库中找不到 Angular html 文件

angular - 无法导航到具有在 Angular2 中按名称指定的 redirectTo 的路由

angular - 如何在命名 socket 是未命名 socket 的子代时导航到路线